Created
December 18, 2012 10:28
-
-
Save leon/4326933 to your computer and use it in GitHub Desktop.
Play 2 EBean SnakeYaml Constructor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Products | |
# | |
- !!models.Product | |
name: Product 1 | |
state: !!models.ProductState 'SPECIFY' | |
rooms: | |
- &kitchen !!models.Room | |
name: Kitchen | |
detailGroups: | |
- &floors !id models.DetailGroup(1) | |
- &fridges !id models.DetailGroup(2) | |
- &bathroom !!models.Room | |
name: Bathroom | |
detailGroups: | |
- *floors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils; | |
import com.avaje.ebean.Ebean; | |
import org.yaml.snakeyaml.constructor.AbstractConstruct; | |
import org.yaml.snakeyaml.constructor.CustomClassLoaderConstructor; | |
import org.yaml.snakeyaml.nodes.Node; | |
import org.yaml.snakeyaml.nodes.ScalarNode; | |
import org.yaml.snakeyaml.nodes.Tag; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
/** | |
* Usage | |
* Since we can't use the built in play Yaml.load we need to create the yaml instance and pass it our custom constructor | |
* | |
* org.yaml.snakeyaml.Yaml yaml = new org.yaml.snakeyaml.Yaml(new YamlEBeanConstructor(play.Play.application().classloader())); | |
* InputStream is = play.Play.application().resourceAsStream("products.yml"); | |
* Map<String, Product> productMap = (Map<String, Product>) yaml.load(is); | |
*/ | |
public class YamlEBeanConstructor extends CustomClassLoaderConstructor { | |
public YamlEBeanConstructor(ClassLoader cLoader) { | |
super(cLoader); | |
yamlConstructors.put(new Tag("!id"), new ConstructEBeanById("default")); | |
} | |
private class ConstructEBeanById extends AbstractConstruct { | |
private String server; | |
private ConstructEBeanById(String server) { | |
this.server = server; | |
} | |
final Pattern classPattern = Pattern.compile("([a-zA-Z.]+)\\((\\d+)\\).*"); | |
public Object construct(Node node) { | |
String val = ((ScalarNode) node).getValue(); | |
Matcher m = classPattern.matcher(val); | |
if (!m.matches()) return null; | |
String className = m.group(1); | |
String id = m.group(2); | |
try { | |
return Ebean.getServer(server).getReference(getClassForName(className), Long.parseLong(id)); | |
} catch (ClassNotFoundException e) { | |
return null; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static List<Product> products = null; | |
public static List<Product> getProducts() { | |
if (products == null) { | |
org.yaml.snakeyaml.Yaml yaml = new org.yaml.snakeyaml.Yaml(new YamlEBeanConstructor(play.Play.application().classloader())); | |
InputStream is = play.Play.application().resourceAsStream("products.yml"); | |
products = (List<Product>) yaml.load(is); | |
} | |
return products; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment