001 /**
002 * <copyright>
003 * </copyright>
004 *
005 * $Id$
006 */
007 package com.hammurapi.config.impl;
008
009 import java.util.Collection;
010 import java.util.HashSet;
011 import java.util.Set;
012 import java.util.logging.Logger;
013
014 import org.eclipse.emf.common.notify.Notification;
015 import org.eclipse.emf.common.notify.NotificationChain;
016 import org.eclipse.emf.common.util.EList;
017 import org.eclipse.emf.ecore.EClass;
018 import org.eclipse.emf.ecore.InternalEObject;
019 import org.eclipse.emf.ecore.impl.ENotificationImpl;
020 import org.eclipse.emf.ecore.impl.EObjectImpl;
021 import org.eclipse.emf.ecore.util.EObjectContainmentEList;
022 import org.eclipse.emf.ecore.util.EObjectContainmentWithInverseEList;
023 import org.eclipse.emf.ecore.util.EcoreUtil;
024 import org.eclipse.emf.ecore.util.InternalEList;
025
026 import com.hammurapi.config.ConfigPackage;
027 import com.hammurapi.config.MethodCall;
028 import com.hammurapi.config.Named;
029 import com.hammurapi.config.Path;
030 import com.hammurapi.config.Profile;
031 import com.hammurapi.config.PropertySource;
032 import com.hammurapi.config.bootstrap.ConfigurationException;
033 import com.hammurapi.config.runtime.InjectionConfig;
034
035 /**
036 * <!-- begin-user-doc -->
037 * An implementation of the model object '<em><b>Profile</b></em>'.
038 * <!-- end-user-doc -->
039 * <p>
040 * The following features are implemented:
041 * <ul>
042 * <li>{@link com.hammurapi.config.impl.ProfileImpl#getProperty <em>Property</em>}</li>
043 * <li>{@link com.hammurapi.config.impl.ProfileImpl#getProfile <em>Profile</em>}</li>
044 * <li>{@link com.hammurapi.config.impl.ProfileImpl#getClassPath <em>Class Path</em>}</li>
045 * <li>{@link com.hammurapi.config.impl.ProfileImpl#getName <em>Name</em>}</li>
046 * <li>{@link com.hammurapi.config.impl.ProfileImpl#getDescription <em>Description</em>}</li>
047 * <li>{@link com.hammurapi.config.impl.ProfileImpl#getOwner <em>Owner</em>}</li>
048 * </ul>
049 * </p>
050 *
051 * @generated
052 */
053 public class ProfileImpl extends EObjectImpl implements Profile {
054 private static final Logger logger = Logger.getLogger(ProfileImpl.class.getName());
055 /**
056 * The cached value of the '{@link #getProperty() <em>Property</em>}' containment reference list.
057 * <!-- begin-user-doc -->
058 * <!-- end-user-doc -->
059 * @see #getProperty()
060 * @generated
061 * @ordered
062 */
063 protected EList<Named> property;
064
065 /**
066 * The cached value of the '{@link #getProfile() <em>Profile</em>}' containment reference list.
067 * <!-- begin-user-doc -->
068 * <!-- end-user-doc -->
069 * @see #getProfile()
070 * @generated
071 * @ordered
072 */
073 protected EList<Profile> profile;
074
075 /**
076 * The cached value of the '{@link #getClassPath() <em>Class Path</em>}' containment reference list.
077 * <!-- begin-user-doc -->
078 * <!-- end-user-doc -->
079 * @see #getClassPath()
080 * @generated
081 * @ordered
082 */
083 protected EList<Path> classPath;
084
085 /**
086 * The default value of the '{@link #getName() <em>Name</em>}' attribute.
087 * <!-- begin-user-doc -->
088 * <!-- end-user-doc -->
089 * @see #getName()
090 * @generated
091 * @ordered
092 */
093 protected static final String NAME_EDEFAULT = null;
094
095 /**
096 * The cached value of the '{@link #getName() <em>Name</em>}' attribute.
097 * <!-- begin-user-doc -->
098 * <!-- end-user-doc -->
099 * @see #getName()
100 * @generated
101 * @ordered
102 */
103 protected String name = NAME_EDEFAULT;
104
105 /**
106 * The default value of the '{@link #getDescription() <em>Description</em>}' attribute.
107 * <!-- begin-user-doc -->
108 * <!-- end-user-doc -->
109 * @see #getDescription()
110 * @generated
111 * @ordered
112 */
113 protected static final String DESCRIPTION_EDEFAULT = null;
114
115 /**
116 * The cached value of the '{@link #getDescription() <em>Description</em>}' attribute.
117 * <!-- begin-user-doc -->
118 * <!-- end-user-doc -->
119 * @see #getDescription()
120 * @generated
121 * @ordered
122 */
123 protected String description = DESCRIPTION_EDEFAULT;
124
125 /**
126 * <!-- begin-user-doc -->
127 * <!-- end-user-doc -->
128 * @generated
129 */
130 protected ProfileImpl() {
131 super();
132 }
133
134 /**
135 * <!-- begin-user-doc -->
136 * <!-- end-user-doc -->
137 * @generated
138 */
139 @Override
140 protected EClass eStaticClass() {
141 return ConfigPackage.Literals.PROFILE;
142 }
143
144 /**
145 * <!-- begin-user-doc -->
146 * <!-- end-user-doc -->
147 * @generated
148 */
149 public EList<Named> getProperty() {
150 if (property == null) {
151 property = new EObjectContainmentEList<Named>(Named.class, this, ConfigPackage.PROFILE__PROPERTY);
152 }
153 return property;
154 }
155
156 /**
157 * <!-- begin-user-doc -->
158 * <!-- end-user-doc -->
159 * @generated
160 */
161 public EList<Profile> getProfile() {
162 if (profile == null) {
163 profile = new EObjectContainmentWithInverseEList<Profile>(Profile.class, this, ConfigPackage.PROFILE__PROFILE, ConfigPackage.PROFILE__OWNER);
164 }
165 return profile;
166 }
167
168 /**
169 * <!-- begin-user-doc -->
170 * <!-- end-user-doc -->
171 * @generated
172 */
173 public EList<Path> getClassPath() {
174 if (classPath == null) {
175 classPath = new EObjectContainmentEList<Path>(Path.class, this, ConfigPackage.PROFILE__CLASS_PATH);
176 }
177 return classPath;
178 }
179
180 /**
181 * <!-- begin-user-doc -->
182 * <!-- end-user-doc -->
183 * @generated
184 */
185 public String getName() {
186 return name;
187 }
188
189 /**
190 * <!-- begin-user-doc -->
191 * <!-- end-user-doc -->
192 * @generated
193 */
194 public void setName(String newName) {
195 String oldName = name;
196 name = newName;
197 if (eNotificationRequired())
198 eNotify(new ENotificationImpl(this, Notification.SET, ConfigPackage.PROFILE__NAME, oldName, name));
199 }
200
201 /**
202 * <!-- begin-user-doc -->
203 * <!-- end-user-doc -->
204 * @generated
205 */
206 public String getDescription() {
207 return description;
208 }
209
210 /**
211 * <!-- begin-user-doc -->
212 * <!-- end-user-doc -->
213 * @generated
214 */
215 public void setDescription(String newDescription) {
216 String oldDescription = description;
217 description = newDescription;
218 if (eNotificationRequired())
219 eNotify(new ENotificationImpl(this, Notification.SET, ConfigPackage.PROFILE__DESCRIPTION, oldDescription, description));
220 }
221
222 /**
223 * <!-- begin-user-doc -->
224 * <!-- end-user-doc -->
225 * @generated
226 */
227 public PropertySource getOwner() {
228 if (eContainerFeatureID() != ConfigPackage.PROFILE__OWNER) return null;
229 return (PropertySource)eContainer();
230 }
231
232 /**
233 * <!-- begin-user-doc -->
234 * <!-- end-user-doc -->
235 * @generated
236 */
237 public NotificationChain basicSetOwner(PropertySource newOwner, NotificationChain msgs) {
238 msgs = eBasicSetContainer((InternalEObject)newOwner, ConfigPackage.PROFILE__OWNER, msgs);
239 return msgs;
240 }
241
242 /**
243 * <!-- begin-user-doc -->
244 * <!-- end-user-doc -->
245 * @generated
246 */
247 public void setOwner(PropertySource newOwner) {
248 if (newOwner != eInternalContainer() || (eContainerFeatureID() != ConfigPackage.PROFILE__OWNER && newOwner != null)) {
249 if (EcoreUtil.isAncestor(this, newOwner))
250 throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
251 NotificationChain msgs = null;
252 if (eInternalContainer() != null)
253 msgs = eBasicRemoveFromContainer(msgs);
254 if (newOwner != null)
255 msgs = ((InternalEObject)newOwner).eInverseAdd(this, ConfigPackage.PROPERTY_SOURCE__PROFILE, PropertySource.class, msgs);
256 msgs = basicSetOwner(newOwner, msgs);
257 if (msgs != null) msgs.dispatch();
258 }
259 else if (eNotificationRequired())
260 eNotify(new ENotificationImpl(this, Notification.SET, ConfigPackage.PROFILE__OWNER, newOwner, newOwner));
261 }
262
263 /**
264 * <!-- begin-user-doc -->
265 * <!-- end-user-doc -->
266 * @generated
267 */
268 @SuppressWarnings("unchecked")
269 @Override
270 public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
271 switch (featureID) {
272 case ConfigPackage.PROFILE__PROFILE:
273 return ((InternalEList<InternalEObject>)(InternalEList<?>)getProfile()).basicAdd(otherEnd, msgs);
274 case ConfigPackage.PROFILE__OWNER:
275 if (eInternalContainer() != null)
276 msgs = eBasicRemoveFromContainer(msgs);
277 return basicSetOwner((PropertySource)otherEnd, msgs);
278 }
279 return super.eInverseAdd(otherEnd, featureID, msgs);
280 }
281
282 /**
283 * <!-- begin-user-doc -->
284 * <!-- end-user-doc -->
285 * @generated
286 */
287 @Override
288 public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
289 switch (featureID) {
290 case ConfigPackage.PROFILE__PROPERTY:
291 return ((InternalEList<?>)getProperty()).basicRemove(otherEnd, msgs);
292 case ConfigPackage.PROFILE__PROFILE:
293 return ((InternalEList<?>)getProfile()).basicRemove(otherEnd, msgs);
294 case ConfigPackage.PROFILE__CLASS_PATH:
295 return ((InternalEList<?>)getClassPath()).basicRemove(otherEnd, msgs);
296 case ConfigPackage.PROFILE__OWNER:
297 return basicSetOwner(null, msgs);
298 }
299 return super.eInverseRemove(otherEnd, featureID, msgs);
300 }
301
302 /**
303 * <!-- begin-user-doc -->
304 * <!-- end-user-doc -->
305 * @generated
306 */
307 @Override
308 public NotificationChain eBasicRemoveFromContainerFeature(NotificationChain msgs) {
309 switch (eContainerFeatureID()) {
310 case ConfigPackage.PROFILE__OWNER:
311 return eInternalContainer().eInverseRemove(this, ConfigPackage.PROPERTY_SOURCE__PROFILE, PropertySource.class, msgs);
312 }
313 return super.eBasicRemoveFromContainerFeature(msgs);
314 }
315
316 /**
317 * <!-- begin-user-doc -->
318 * <!-- end-user-doc -->
319 * @generated
320 */
321 @Override
322 public Object eGet(int featureID, boolean resolve, boolean coreType) {
323 switch (featureID) {
324 case ConfigPackage.PROFILE__PROPERTY:
325 return getProperty();
326 case ConfigPackage.PROFILE__PROFILE:
327 return getProfile();
328 case ConfigPackage.PROFILE__CLASS_PATH:
329 return getClassPath();
330 case ConfigPackage.PROFILE__NAME:
331 return getName();
332 case ConfigPackage.PROFILE__DESCRIPTION:
333 return getDescription();
334 case ConfigPackage.PROFILE__OWNER:
335 return getOwner();
336 }
337 return super.eGet(featureID, resolve, coreType);
338 }
339
340 /**
341 * <!-- begin-user-doc -->
342 * <!-- end-user-doc -->
343 * @generated
344 */
345 @SuppressWarnings("unchecked")
346 @Override
347 public void eSet(int featureID, Object newValue) {
348 switch (featureID) {
349 case ConfigPackage.PROFILE__PROPERTY:
350 getProperty().clear();
351 getProperty().addAll((Collection<? extends Named>)newValue);
352 return;
353 case ConfigPackage.PROFILE__PROFILE:
354 getProfile().clear();
355 getProfile().addAll((Collection<? extends Profile>)newValue);
356 return;
357 case ConfigPackage.PROFILE__CLASS_PATH:
358 getClassPath().clear();
359 getClassPath().addAll((Collection<? extends Path>)newValue);
360 return;
361 case ConfigPackage.PROFILE__NAME:
362 setName((String)newValue);
363 return;
364 case ConfigPackage.PROFILE__DESCRIPTION:
365 setDescription((String)newValue);
366 return;
367 case ConfigPackage.PROFILE__OWNER:
368 setOwner((PropertySource)newValue);
369 return;
370 }
371 super.eSet(featureID, newValue);
372 }
373
374 /**
375 * <!-- begin-user-doc -->
376 * <!-- end-user-doc -->
377 * @generated
378 */
379 @Override
380 public void eUnset(int featureID) {
381 switch (featureID) {
382 case ConfigPackage.PROFILE__PROPERTY:
383 getProperty().clear();
384 return;
385 case ConfigPackage.PROFILE__PROFILE:
386 getProfile().clear();
387 return;
388 case ConfigPackage.PROFILE__CLASS_PATH:
389 getClassPath().clear();
390 return;
391 case ConfigPackage.PROFILE__NAME:
392 setName(NAME_EDEFAULT);
393 return;
394 case ConfigPackage.PROFILE__DESCRIPTION:
395 setDescription(DESCRIPTION_EDEFAULT);
396 return;
397 case ConfigPackage.PROFILE__OWNER:
398 setOwner((PropertySource)null);
399 return;
400 }
401 super.eUnset(featureID);
402 }
403
404 /**
405 * <!-- begin-user-doc -->
406 * <!-- end-user-doc -->
407 * @generated
408 */
409 @Override
410 public boolean eIsSet(int featureID) {
411 switch (featureID) {
412 case ConfigPackage.PROFILE__PROPERTY:
413 return property != null && !property.isEmpty();
414 case ConfigPackage.PROFILE__PROFILE:
415 return profile != null && !profile.isEmpty();
416 case ConfigPackage.PROFILE__CLASS_PATH:
417 return classPath != null && !classPath.isEmpty();
418 case ConfigPackage.PROFILE__NAME:
419 return NAME_EDEFAULT == null ? name != null : !NAME_EDEFAULT.equals(name);
420 case ConfigPackage.PROFILE__DESCRIPTION:
421 return DESCRIPTION_EDEFAULT == null ? description != null : !DESCRIPTION_EDEFAULT.equals(description);
422 case ConfigPackage.PROFILE__OWNER:
423 return getOwner() != null;
424 }
425 return super.eIsSet(featureID);
426 }
427
428 /**
429 * <!-- begin-user-doc -->
430 * <!-- end-user-doc -->
431 * @generated
432 */
433 @Override
434 public String toString() {
435 if (eIsProxy()) return super.toString();
436
437 StringBuffer result = new StringBuffer(super.toString());
438 result.append(" (name: ");
439 result.append(name);
440 result.append(", description: ");
441 result.append(description);
442 result.append(')');
443 return result.toString();
444 }
445
446 /**
447 * Generates property name for property. For all Named subclasses except MethodCall it is just property name.
448 * For method call it is <code><name>[<number of arguments>]</code>
449 * @param property
450 * @return Property name.
451 */
452 static String propertyName(Named property) {
453 if (property instanceof MethodCall) {
454 MethodCall mc = (MethodCall) property;
455 return mc.getName()+"["+mc.getArgument().size()+"]";
456 }
457
458 return property.getName();
459 }
460
461 public void injectProperties(final InjectionConfig injectionConfig) throws ConfigurationException {
462
463 Set<String> allInjected = new HashSet<String>(injectionConfig.getAlreadyInjected());
464 for (Named property: getProperty()) {
465 String propertyName = propertyName(property);
466 if (property.isEnabled() && property.isRuntime() && !injectionConfig.getAlreadyInjected().contains(propertyName)) {
467 allInjected.add(propertyName);
468 logger.fine("Injecting "+propertyName+" from profile "+getName());
469 property.inject(injectionConfig);
470 }
471 }
472
473 if (getOwner()!=null) {
474 try {
475 InjectionConfig oic = (InjectionConfig) injectionConfig.clone();
476 oic.setAlreadyInjected(allInjected);
477 getOwner().injectProperties(oic);
478 } catch (CloneNotSupportedException e) {
479 throw new ConfigurationException(e);
480 }
481 }
482 }
483
484 } //ProfileImpl