001 package com.hammurapi.config.util;
002
003 import java.io.File;
004 import java.io.IOException;
005 import java.util.ArrayList;
006 import java.util.logging.Logger;
007
008 import org.eclipse.emf.common.util.EList;
009 import org.eclipse.emf.common.util.URI;
010 import org.eclipse.emf.ecore.EObject;
011 import org.eclipse.emf.ecore.resource.Resource;
012 import org.eclipse.emf.ecore.resource.ResourceSet;
013 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
014 import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
015
016 import com.hammurapi.config.ConfigFactory;
017 import com.hammurapi.config.Factory;
018 import com.hammurapi.config.Named;
019 import com.hammurapi.config.NamedObjectDefinition;
020 import com.hammurapi.config.ObjectDefinition;
021 import com.hammurapi.config.Path;
022 import com.hammurapi.config.Profile;
023 import com.hammurapi.config.PropertySource;
024 import com.hammurapi.config.Source;
025 import com.hammurapi.party.Role;
026
027 /**
028 * Helper class.
029 * @author Pavel Vlasov
030 *
031 */
032 public class ConfigUtil {
033 private static final Logger logger = Logger.getLogger(ConfigUtil.class.getName());
034
035 /**
036 * Moves references/copy artifacts from one object to another.
037 * @param source
038 * @param dest
039 */
040 public static void pump(Factory source, Factory dest) {
041 dest.getSource().addAll(new ArrayList<Source>(source.getSource()));
042 dest.setConfigScript(source.getConfigScript());
043 dest.setDescription(source.getDescription());
044 dest.setId(source.getId());
045
046 dest.getRoles().clear();
047 dest.getRoles().addAll(new ArrayList<Role>(source.getRoles()));
048
049 dest.setHomePage(source.getHomePage());
050
051 dest.getSupportedExecutionEnvironments().clear();
052 dest.getSupportedExecutionEnvironments().addAll(new ArrayList<String>(source.getSupportedExecutionEnvironments()));
053 }
054
055 /**
056 * Moves references/copy artifacts from one object to another.
057 * @param source
058 * @param dest
059 */
060 public static void pump(PropertySource source, PropertySource dest) {
061 dest.getClassPath().addAll(new ArrayList<Path>(source.getClassPath()));
062 dest.getProfile().addAll(new ArrayList<Profile>(source.getProfile()));
063 dest.getProperty().addAll(new ArrayList<Named>(source.getProperty()));
064 }
065
066 /**
067 * Moves references/copy artifacts from one object to another.
068 * @param source
069 * @param dest
070 */
071 public static void pump(ObjectDefinition source, ObjectDefinition dest) {
072 pump((Factory) source, (Factory) dest);
073 pump((PropertySource) source, (PropertySource) dest);
074
075 dest.setConstructor(source.getConstructor());
076 dest.setType(source.getType());
077 dest.setValue(source.getValue());
078 }
079
080 /**
081 * Moves references/copy artifacts from one object to another.
082 * @param source
083 * @param dest
084 */
085 public static void pump(Named source, Named dest) {
086 pump((Factory) source, (Factory) dest);
087 dest.setName(source.getName());
088 dest.setRuntime(source.isRuntime());
089 }
090
091 /**
092 * Moves references/copy artifacts from one object to another.
093 * @param source
094 * @param dest
095 */
096 public static void pump(NamedObjectDefinition source, NamedObjectDefinition dest) {
097 pump((ObjectDefinition) source, (ObjectDefinition) dest);
098 pump((Named) source, (Named) dest);
099 }
100
101 /**
102 * Adds scalar (type, value) property.
103 * @param target
104 * @param name
105 * @param type
106 * @param value
107 * @param runtime
108 */
109 public static void addScalarProperty(PropertySource target, String name, String type, String value, boolean runtime) {
110 if (value!=null) {
111 logger.fine("Adding scalar property "+name+" of type "+type+", value='"+value+"'");
112 NamedObjectDefinition prop = ConfigFactory.eINSTANCE.createNamedObjectDefinition();
113 prop.setName(name);
114 prop.setType(type);
115 prop.setValue(value);
116 prop.setRuntime(runtime);
117 target.getProperty().add(prop);
118 }
119 }
120
121 /**
122 * Adds scalar property with type of property type and value of property.toString().
123 * @param target
124 * @param name
125 * @param property
126 * @param runtime
127 */
128 public static void addScalarProperty(PropertySource target, String name, Object property, boolean runtime) {
129 if (property!=null) {
130 addScalarProperty(target, name, property.getClass().getName(), property instanceof Class<?> ? ((Class<?>) property).getName() : property.toString(), runtime);
131 }
132 }
133
134 /**
135 * Saves EObject to file.
136 * @param obj Object to save.
137 * @param file Output file.
138 * @throws IOException
139 */
140 public static void save(EObject obj, File file) throws IOException {
141 ResourceSet resourceSet = new ResourceSetImpl();
142 // Register the appropriate resource factory to handle all file extensions.
143 //
144 resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(
145 Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
146
147 URI uri = URI.createFileURI(file.getAbsolutePath());
148 Resource configResource = resourceSet.createResource(uri);
149 EList<EObject> contents = configResource.getContents();
150 contents.add(obj);
151 configResource.save(null);
152
153 }
154
155
156 }