001    /**
002     * <copyright>
003     * </copyright>
004     *
005     * $Id$
006     */
007    package com.hammurapi.config.impl;
008    
009    import java.io.File;
010    import java.util.Collection;
011    import java.util.logging.Logger;
012    
013    import org.codehaus.janino.ScriptEvaluator;
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.EDataTypeUniqueEList;
022    import org.eclipse.emf.ecore.util.EObjectContainmentEList;
023    import org.eclipse.emf.ecore.util.EObjectContainmentWithInverseEList;
024    import org.eclipse.emf.ecore.util.InternalEList;
025    
026    import com.hammurapi.config.ConfigPackage;
027    import com.hammurapi.config.Map;
028    import com.hammurapi.config.MapEntry;
029    import com.hammurapi.config.NamedMap;
030    import com.hammurapi.config.Source;
031    import com.hammurapi.config.bootstrap.ConfigurationException;
032    import com.hammurapi.config.bootstrap.FactoryClosure;
033    import com.hammurapi.config.bootstrap.TokenExpander.TokenSource;
034    import com.hammurapi.config.runtime.Component;
035    import com.hammurapi.config.runtime.ConfigurationContext;
036    import com.hammurapi.config.runtime.FactoryConfig;
037    import com.hammurapi.config.runtime.FactoryClosureBase;
038    import com.hammurapi.config.runtime.InjectionConfig;
039    import com.hammurapi.party.PartyPackage;
040    import com.hammurapi.party.Role;
041    
042    /**
043     * <!-- begin-user-doc -->
044     * An implementation of the model object '<em><b>Named Map</b></em>'.
045     * <!-- end-user-doc -->
046     * <p>
047     * The following features are implemented:
048     * <ul>
049     *   <li>{@link com.hammurapi.config.impl.NamedMapImpl#getRoles <em>Roles</em>}</li>
050     *   <li>{@link com.hammurapi.config.impl.NamedMapImpl#getDescription <em>Description</em>}</li>
051     *   <li>{@link com.hammurapi.config.impl.NamedMapImpl#getConfigScript <em>Config Script</em>}</li>
052     *   <li>{@link com.hammurapi.config.impl.NamedMapImpl#getId <em>Id</em>}</li>
053     *   <li>{@link com.hammurapi.config.impl.NamedMapImpl#getSource <em>Source</em>}</li>
054     *   <li>{@link com.hammurapi.config.impl.NamedMapImpl#isEnabled <em>Enabled</em>}</li>
055     *   <li>{@link com.hammurapi.config.impl.NamedMapImpl#getService <em>Service</em>}</li>
056     *   <li>{@link com.hammurapi.config.impl.NamedMapImpl#getSupportedExecutionEnvironments <em>Supported Execution Environments</em>}</li>
057     *   <li>{@link com.hammurapi.config.impl.NamedMapImpl#getHomePage <em>Home Page</em>}</li>
058     *   <li>{@link com.hammurapi.config.impl.NamedMapImpl#getName <em>Name</em>}</li>
059     *   <li>{@link com.hammurapi.config.impl.NamedMapImpl#isRuntime <em>Runtime</em>}</li>
060     *   <li>{@link com.hammurapi.config.impl.NamedMapImpl#getEntry <em>Entry</em>}</li>
061     *   <li>{@link com.hammurapi.config.impl.NamedMapImpl#getType <em>Type</em>}</li>
062     *   <li>{@link com.hammurapi.config.impl.NamedMapImpl#isFactoryMap <em>Factory Map</em>}</li>
063     * </ul>
064     * </p>
065     *
066     * @generated
067     */
068    public class NamedMapImpl extends EObjectImpl implements NamedMap {
069            /**
070             * The cached value of the '{@link #getRoles() <em>Roles</em>}' containment reference list.
071             * <!-- begin-user-doc -->
072             * <!-- end-user-doc -->
073             * @see #getRoles()
074             * @generated
075             * @ordered
076             */
077            protected EList<Role> roles;
078            private static final Logger logger = Logger.getLogger(NamedMapImpl.class.getName());
079            /**
080             * The default value of the '{@link #getDescription() <em>Description</em>}' attribute.
081             * <!-- begin-user-doc -->
082             * <!-- end-user-doc -->
083             * @see #getDescription()
084             * @generated
085             * @ordered
086             */
087            protected static final String DESCRIPTION_EDEFAULT = null;
088    
089            /**
090             * The cached value of the '{@link #getDescription() <em>Description</em>}' attribute.
091             * <!-- begin-user-doc -->
092             * <!-- end-user-doc -->
093             * @see #getDescription()
094             * @generated
095             * @ordered
096             */
097            protected String description = DESCRIPTION_EDEFAULT;
098    
099            /**
100             * The default value of the '{@link #getConfigScript() <em>Config Script</em>}' attribute.
101             * <!-- begin-user-doc -->
102             * <!-- end-user-doc -->
103             * @see #getConfigScript()
104             * @generated
105             * @ordered
106             */
107            protected static final String CONFIG_SCRIPT_EDEFAULT = null;
108    
109            /**
110             * The cached value of the '{@link #getConfigScript() <em>Config Script</em>}' attribute.
111             * <!-- begin-user-doc -->
112             * <!-- end-user-doc -->
113             * @see #getConfigScript()
114             * @generated
115             * @ordered
116             */
117            protected String configScript = CONFIG_SCRIPT_EDEFAULT;
118    
119            /**
120             * The default value of the '{@link #getId() <em>Id</em>}' attribute.
121             * <!-- begin-user-doc -->
122             * <!-- end-user-doc -->
123             * @see #getId()
124             * @generated
125             * @ordered
126             */
127            protected static final String ID_EDEFAULT = null;
128    
129            /**
130             * The cached value of the '{@link #getId() <em>Id</em>}' attribute.
131             * <!-- begin-user-doc -->
132             * <!-- end-user-doc -->
133             * @see #getId()
134             * @generated
135             * @ordered
136             */
137            protected String id = ID_EDEFAULT;
138    
139            /**
140             * The cached value of the '{@link #getSource() <em>Source</em>}' containment reference list.
141             * <!-- begin-user-doc -->
142             * <!-- end-user-doc -->
143             * @see #getSource()
144             * @generated
145             * @ordered
146             */
147            protected EList<Source> source;
148    
149            /**
150             * The default value of the '{@link #isEnabled() <em>Enabled</em>}' attribute.
151             * <!-- begin-user-doc -->
152             * <!-- end-user-doc -->
153             * @see #isEnabled()
154             * @generated
155             * @ordered
156             */
157            protected static final boolean ENABLED_EDEFAULT = true;
158    
159            /**
160             * The cached value of the '{@link #isEnabled() <em>Enabled</em>}' attribute.
161             * <!-- begin-user-doc -->
162             * <!-- end-user-doc -->
163             * @see #isEnabled()
164             * @generated
165             * @ordered
166             */
167            protected boolean enabled = ENABLED_EDEFAULT;
168    
169            /**
170             * The cached value of the '{@link #getService() <em>Service</em>}' attribute list.
171             * <!-- begin-user-doc -->
172             * <!-- end-user-doc -->
173             * @see #getService()
174             * @generated
175             * @ordered
176             */
177            protected EList<String> service;
178    
179            /**
180             * The cached value of the '{@link #getSupportedExecutionEnvironments() <em>Supported Execution Environments</em>}' attribute list.
181             * <!-- begin-user-doc -->
182             * <!-- end-user-doc -->
183             * @see #getSupportedExecutionEnvironments()
184             * @generated
185             * @ordered
186             */
187            protected EList<String> supportedExecutionEnvironments;
188            /**
189             * The default value of the '{@link #getHomePage() <em>Home Page</em>}' attribute.
190             * <!-- begin-user-doc -->
191             * <!-- end-user-doc -->
192             * @see #getHomePage()
193             * @generated
194             * @ordered
195             */
196            protected static final String HOME_PAGE_EDEFAULT = null;
197            /**
198             * The cached value of the '{@link #getHomePage() <em>Home Page</em>}' attribute.
199             * <!-- begin-user-doc -->
200             * <!-- end-user-doc -->
201             * @see #getHomePage()
202             * @generated
203             * @ordered
204             */
205            protected String homePage = HOME_PAGE_EDEFAULT;
206            /**
207             * The default value of the '{@link #getName() <em>Name</em>}' attribute.
208             * <!-- begin-user-doc -->
209             * <!-- end-user-doc -->
210             * @see #getName()
211             * @generated
212             * @ordered
213             */
214            protected static final String NAME_EDEFAULT = null;
215    
216            /**
217             * The cached value of the '{@link #getName() <em>Name</em>}' attribute.
218             * <!-- begin-user-doc -->
219             * <!-- end-user-doc -->
220             * @see #getName()
221             * @generated
222             * @ordered
223             */
224            protected String name = NAME_EDEFAULT;
225    
226            /**
227             * The default value of the '{@link #isRuntime() <em>Runtime</em>}' attribute.
228             * <!-- begin-user-doc -->
229             * <!-- end-user-doc -->
230             * @see #isRuntime()
231             * @generated
232             * @ordered
233             */
234            protected static final boolean RUNTIME_EDEFAULT = true;
235    
236            /**
237             * The cached value of the '{@link #isRuntime() <em>Runtime</em>}' attribute.
238             * <!-- begin-user-doc -->
239             * <!-- end-user-doc -->
240             * @see #isRuntime()
241             * @generated
242             * @ordered
243             */
244            protected boolean runtime = RUNTIME_EDEFAULT;
245    
246            /**
247             * The cached value of the '{@link #getEntry() <em>Entry</em>}' containment reference list.
248             * <!-- begin-user-doc -->
249             * <!-- end-user-doc -->
250             * @see #getEntry()
251             * @generated
252             * @ordered
253             */
254            protected EList<MapEntry> entry;
255    
256            /**
257             * The default value of the '{@link #getType() <em>Type</em>}' attribute.
258             * <!-- begin-user-doc -->
259             * <!-- end-user-doc -->
260             * @see #getType()
261             * @generated
262             * @ordered
263             */
264            protected static final String TYPE_EDEFAULT = "java.util.HashMap";
265    
266            /**
267             * The cached value of the '{@link #getType() <em>Type</em>}' attribute.
268             * <!-- begin-user-doc -->
269             * <!-- end-user-doc -->
270             * @see #getType()
271             * @generated
272             * @ordered
273             */
274            protected String type = TYPE_EDEFAULT;
275    
276            /**
277             * The default value of the '{@link #isFactoryMap() <em>Factory Map</em>}' attribute.
278             * <!-- begin-user-doc -->
279             * <!-- end-user-doc -->
280             * @see #isFactoryMap()
281             * @generated
282             * @ordered
283             */
284            protected static final boolean FACTORY_MAP_EDEFAULT = false;
285    
286            /**
287             * The cached value of the '{@link #isFactoryMap() <em>Factory Map</em>}' attribute.
288             * <!-- begin-user-doc -->
289             * <!-- end-user-doc -->
290             * @see #isFactoryMap()
291             * @generated
292             * @ordered
293             */
294            protected boolean factoryMap = FACTORY_MAP_EDEFAULT;
295    
296            /**
297             * <!-- begin-user-doc -->
298             * <!-- end-user-doc -->
299             * @generated
300             */
301            protected NamedMapImpl() {
302                    super();
303            }
304    
305            /**
306             * <!-- begin-user-doc -->
307             * <!-- end-user-doc -->
308             * @generated
309             */
310            @Override
311            protected EClass eStaticClass() {
312                    return ConfigPackage.Literals.NAMED_MAP;
313            }
314    
315            /**
316             * <!-- begin-user-doc -->
317             * <!-- end-user-doc -->
318             * @generated
319             */
320            public EList<Role> getRoles() {
321                    if (roles == null) {
322                            roles = new EObjectContainmentWithInverseEList<Role>(Role.class, this, ConfigPackage.NAMED_MAP__ROLES, PartyPackage.ROLE__OWNER);
323                    }
324                    return roles;
325            }
326    
327            /**
328             * <!-- begin-user-doc -->
329             * <!-- end-user-doc -->
330             * @generated
331             */
332            public String getDescription() {
333                    return description;
334            }
335    
336            /**
337             * <!-- begin-user-doc -->
338             * <!-- end-user-doc -->
339             * @generated
340             */
341            public void setDescription(String newDescription) {
342                    String oldDescription = description;
343                    description = newDescription;
344                    if (eNotificationRequired())
345                            eNotify(new ENotificationImpl(this, Notification.SET, ConfigPackage.NAMED_MAP__DESCRIPTION, oldDescription, description));
346            }
347    
348            /**
349             * <!-- begin-user-doc -->
350             * <!-- end-user-doc -->
351             * @generated
352             */
353            public String getConfigScript() {
354                    return configScript;
355            }
356    
357            /**
358             * <!-- begin-user-doc -->
359             * <!-- end-user-doc -->
360             * @generated
361             */
362            public void setConfigScript(String newConfigScript) {
363                    String oldConfigScript = configScript;
364                    configScript = newConfigScript;
365                    if (eNotificationRequired())
366                            eNotify(new ENotificationImpl(this, Notification.SET, ConfigPackage.NAMED_MAP__CONFIG_SCRIPT, oldConfigScript, configScript));
367            }
368    
369            /**
370             * <!-- begin-user-doc -->
371             * <!-- end-user-doc -->
372             * @generated
373             */
374            public String getId() {
375                    return id;
376            }
377    
378            /**
379             * <!-- begin-user-doc -->
380             * <!-- end-user-doc -->
381             * @generated
382             */
383            public void setId(String newId) {
384                    String oldId = id;
385                    id = newId;
386                    if (eNotificationRequired())
387                            eNotify(new ENotificationImpl(this, Notification.SET, ConfigPackage.NAMED_MAP__ID, oldId, id));
388            }
389    
390            /**
391             * <!-- begin-user-doc -->
392             * <!-- end-user-doc -->
393             * @generated
394             */
395            public EList<Source> getSource() {
396                    if (source == null) {
397                            source = new EObjectContainmentEList<Source>(Source.class, this, ConfigPackage.NAMED_MAP__SOURCE);
398                    }
399                    return source;
400            }
401    
402            /**
403             * <!-- begin-user-doc -->
404             * <!-- end-user-doc -->
405             * @generated
406             */
407            public boolean isEnabled() {
408                    return enabled;
409            }
410    
411            /**
412             * <!-- begin-user-doc -->
413             * <!-- end-user-doc -->
414             * @generated
415             */
416            public void setEnabled(boolean newEnabled) {
417                    boolean oldEnabled = enabled;
418                    enabled = newEnabled;
419                    if (eNotificationRequired())
420                            eNotify(new ENotificationImpl(this, Notification.SET, ConfigPackage.NAMED_MAP__ENABLED, oldEnabled, enabled));
421            }
422    
423            /**
424             * <!-- begin-user-doc -->
425             * <!-- end-user-doc -->
426             * @generated
427             */
428            public EList<String> getService() {
429                    if (service == null) {
430                            service = new EDataTypeUniqueEList<String>(String.class, this, ConfigPackage.NAMED_MAP__SERVICE);
431                    }
432                    return service;
433            }
434    
435            /**
436             * <!-- begin-user-doc -->
437             * <!-- end-user-doc -->
438             * @generated
439             */
440            public EList<String> getSupportedExecutionEnvironments() {
441                    if (supportedExecutionEnvironments == null) {
442                            supportedExecutionEnvironments = new EDataTypeUniqueEList<String>(String.class, this, ConfigPackage.NAMED_MAP__SUPPORTED_EXECUTION_ENVIRONMENTS);
443                    }
444                    return supportedExecutionEnvironments;
445            }
446    
447            /**
448             * <!-- begin-user-doc -->
449             * <!-- end-user-doc -->
450             * @generated
451             */
452            public String getHomePage() {
453                    return homePage;
454            }
455    
456            /**
457             * <!-- begin-user-doc -->
458             * <!-- end-user-doc -->
459             * @generated
460             */
461            public void setHomePage(String newHomePage) {
462                    String oldHomePage = homePage;
463                    homePage = newHomePage;
464                    if (eNotificationRequired())
465                            eNotify(new ENotificationImpl(this, Notification.SET, ConfigPackage.NAMED_MAP__HOME_PAGE, oldHomePage, homePage));
466            }
467    
468            /**
469             * <!-- begin-user-doc -->
470             * <!-- end-user-doc -->
471             * @generated
472             */
473            public String getName() {
474                    return name;
475            }
476    
477            /**
478             * <!-- begin-user-doc -->
479             * <!-- end-user-doc -->
480             * @generated
481             */
482            public void setName(String newName) {
483                    String oldName = name;
484                    name = newName;
485                    if (eNotificationRequired())
486                            eNotify(new ENotificationImpl(this, Notification.SET, ConfigPackage.NAMED_MAP__NAME, oldName, name));
487            }
488    
489            /**
490             * <!-- begin-user-doc -->
491             * <!-- end-user-doc -->
492             * @generated
493             */
494            public boolean isRuntime() {
495                    return runtime;
496            }
497    
498            /**
499             * <!-- begin-user-doc -->
500             * <!-- end-user-doc -->
501             * @generated
502             */
503            public void setRuntime(boolean newRuntime) {
504                    boolean oldRuntime = runtime;
505                    runtime = newRuntime;
506                    if (eNotificationRequired())
507                            eNotify(new ENotificationImpl(this, Notification.SET, ConfigPackage.NAMED_MAP__RUNTIME, oldRuntime, runtime));
508            }
509    
510            /**
511             * <!-- begin-user-doc -->
512             * <!-- end-user-doc -->
513             * @generated
514             */
515            public EList<MapEntry> getEntry() {
516                    if (entry == null) {
517                            entry = new EObjectContainmentEList<MapEntry>(MapEntry.class, this, ConfigPackage.NAMED_MAP__ENTRY);
518                    }
519                    return entry;
520            }
521    
522            /**
523             * <!-- begin-user-doc -->
524             * <!-- end-user-doc -->
525             * @generated
526             */
527            public String getType() {
528                    return type;
529            }
530    
531            /**
532             * <!-- begin-user-doc -->
533             * <!-- end-user-doc -->
534             * @generated
535             */
536            public void setType(String newType) {
537                    String oldType = type;
538                    type = newType;
539                    if (eNotificationRequired())
540                            eNotify(new ENotificationImpl(this, Notification.SET, ConfigPackage.NAMED_MAP__TYPE, oldType, type));
541            }
542    
543            /**
544             * <!-- begin-user-doc -->
545             * <!-- end-user-doc -->
546             * @generated
547             */
548            public boolean isFactoryMap() {
549                    return factoryMap;
550            }
551    
552            /**
553             * <!-- begin-user-doc -->
554             * <!-- end-user-doc -->
555             * @generated
556             */
557            public void setFactoryMap(boolean newFactoryMap) {
558                    boolean oldFactoryMap = factoryMap;
559                    factoryMap = newFactoryMap;
560                    if (eNotificationRequired())
561                            eNotify(new ENotificationImpl(this, Notification.SET, ConfigPackage.NAMED_MAP__FACTORY_MAP, oldFactoryMap, factoryMap));
562            }
563    
564            /**
565             * <!-- begin-user-doc -->
566             * <!-- end-user-doc -->
567             * @generated NOT
568             */
569            @SuppressWarnings("unchecked")
570            public FactoryClosure<Object> create(final FactoryConfig factoryConfig) throws ConfigurationException {
571                    return new FactoryClosureBase<Object>() {
572    
573                            public Object create(Object... args) throws ConfigurationException {                            
574                                    logger.fine("Creating a new named map of type "+getType());
575            
576                                    ClassLoader cl = factoryConfig.getClassLoader();
577                                    if (cl==null) {
578                                            cl = getClass().getClassLoader();
579                                    }
580                                    final ClassLoader theClassLoader = cl;
581                                    try {
582                                            final java.util.Map<Object, Object> map = (java.util.Map<Object, Object>) ObjectDefinitionImpl.construct(cl.loadClass(getType()), args);
583                                            for (MapEntry entry: getEntry()) {
584                                                    if (entry.getKey().isEnabled()) {
585                                                            logger.fine("Adding new entry");
586                                                            
587                                                            FactoryClosure<Object> kfr = entry.getKey().create(factoryConfig);
588                                                            FactoryClosure<Object> vfr = entry.getValue().create(factoryConfig);                              
589                                                            map.put(kfr.create(), isFactoryMap() ? vfr : vfr.create());
590                                                            destroyableSink.addDestroyable(kfr.getDestroyable());
591                                                            destroyableSink.addDestroyable(vfr.getDestroyable());
592                                                    }
593                                            }
594                                            
595                                            NamedMap def = NamedMapImpl.this;
596                                            // Init.
597                                            if (map instanceof Component) {
598                                                    Component<Map> comp = (Component<Map>) map;
599                                                    ConfigurationContext<Map> context = (ConfigurationContext<Map>) factoryConfig.getContextFactory().createContext(def, comp, factoryConfig, destroyableSink); 
600                                                    comp.init(context);
601                                            }
602                                            
603                                            // Execute script once all keys and values are initialized
604                                            if (getConfigScript()!=null && getConfigScript().trim().length()>0) {
605                                                    ScriptEvaluator se = new ScriptEvaluator();
606                                                    se.setParameters(new String[] {"instance", "definition", "config"}, new Class[] {map.getClass(), def.getClass(), factoryConfig.getClass()});
607                                                    se.setParentClassLoader(theClassLoader);
608                                                    se.setThrownExceptions(new Class[] {ConfigurationException.class});
609                                                    se.cook(getConfigScript());
610                                                    se.evaluate(new Object[] {map, def, factoryConfig});
611                                            }
612                                            
613                                            ObjectDefinitionImpl.expose(NamedMapImpl.this, map, factoryConfig.getContext(), theClassLoader, destroyableSink);                                       
614                                            toDestroy(map);
615                                            return map;
616                                    } catch (ConfigurationException e) {
617                                            throw e;
618                                    } catch (Exception e) {
619                                            throw new ConfigurationException(e);
620                                    }
621                            }
622                            
623                    };
624            }
625    
626            /**
627             * <!-- begin-user-doc -->
628             * <!-- end-user-doc -->
629             * @generated
630             */
631            public void compile(EList<String> profilePath, TokenSource tokens, ClassLoader classLoader, File dir, String targetPackage, String targetClass) throws ConfigurationException {
632                    // TODO: implement this method
633                    // Ensure that you remove @generated or mark it @generated NOT
634                    throw new UnsupportedOperationException();
635            }
636    
637            /**
638             * <!-- begin-user-doc -->
639             * <!-- end-user-doc -->
640             * @generated
641             */
642            @SuppressWarnings("unchecked")
643            @Override
644            public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
645                    switch (featureID) {
646                            case ConfigPackage.NAMED_MAP__ROLES:
647                                    return ((InternalEList<InternalEObject>)(InternalEList<?>)getRoles()).basicAdd(otherEnd, msgs);
648                    }
649                    return super.eInverseAdd(otherEnd, featureID, msgs);
650            }
651    
652            /**
653             * <!-- begin-user-doc -->
654             * <!-- end-user-doc -->
655             * @generated
656             */
657            @Override
658            public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
659                    switch (featureID) {
660                            case ConfigPackage.NAMED_MAP__ROLES:
661                                    return ((InternalEList<?>)getRoles()).basicRemove(otherEnd, msgs);
662                            case ConfigPackage.NAMED_MAP__SOURCE:
663                                    return ((InternalEList<?>)getSource()).basicRemove(otherEnd, msgs);
664                            case ConfigPackage.NAMED_MAP__ENTRY:
665                                    return ((InternalEList<?>)getEntry()).basicRemove(otherEnd, msgs);
666                    }
667                    return super.eInverseRemove(otherEnd, featureID, msgs);
668            }
669    
670            /**
671             * <!-- begin-user-doc -->
672             * <!-- end-user-doc -->
673             * @generated
674             */
675            @Override
676            public Object eGet(int featureID, boolean resolve, boolean coreType) {
677                    switch (featureID) {
678                            case ConfigPackage.NAMED_MAP__ROLES:
679                                    return getRoles();
680                            case ConfigPackage.NAMED_MAP__DESCRIPTION:
681                                    return getDescription();
682                            case ConfigPackage.NAMED_MAP__CONFIG_SCRIPT:
683                                    return getConfigScript();
684                            case ConfigPackage.NAMED_MAP__ID:
685                                    return getId();
686                            case ConfigPackage.NAMED_MAP__SOURCE:
687                                    return getSource();
688                            case ConfigPackage.NAMED_MAP__ENABLED:
689                                    return isEnabled();
690                            case ConfigPackage.NAMED_MAP__SERVICE:
691                                    return getService();
692                            case ConfigPackage.NAMED_MAP__SUPPORTED_EXECUTION_ENVIRONMENTS:
693                                    return getSupportedExecutionEnvironments();
694                            case ConfigPackage.NAMED_MAP__HOME_PAGE:
695                                    return getHomePage();
696                            case ConfigPackage.NAMED_MAP__NAME:
697                                    return getName();
698                            case ConfigPackage.NAMED_MAP__RUNTIME:
699                                    return isRuntime();
700                            case ConfigPackage.NAMED_MAP__ENTRY:
701                                    return getEntry();
702                            case ConfigPackage.NAMED_MAP__TYPE:
703                                    return getType();
704                            case ConfigPackage.NAMED_MAP__FACTORY_MAP:
705                                    return isFactoryMap();
706                    }
707                    return super.eGet(featureID, resolve, coreType);
708            }
709    
710            /**
711             * <!-- begin-user-doc -->
712             * <!-- end-user-doc -->
713             * @generated
714             */
715            @SuppressWarnings("unchecked")
716            @Override
717            public void eSet(int featureID, Object newValue) {
718                    switch (featureID) {
719                            case ConfigPackage.NAMED_MAP__ROLES:
720                                    getRoles().clear();
721                                    getRoles().addAll((Collection<? extends Role>)newValue);
722                                    return;
723                            case ConfigPackage.NAMED_MAP__DESCRIPTION:
724                                    setDescription((String)newValue);
725                                    return;
726                            case ConfigPackage.NAMED_MAP__CONFIG_SCRIPT:
727                                    setConfigScript((String)newValue);
728                                    return;
729                            case ConfigPackage.NAMED_MAP__ID:
730                                    setId((String)newValue);
731                                    return;
732                            case ConfigPackage.NAMED_MAP__SOURCE:
733                                    getSource().clear();
734                                    getSource().addAll((Collection<? extends Source>)newValue);
735                                    return;
736                            case ConfigPackage.NAMED_MAP__ENABLED:
737                                    setEnabled((Boolean)newValue);
738                                    return;
739                            case ConfigPackage.NAMED_MAP__SERVICE:
740                                    getService().clear();
741                                    getService().addAll((Collection<? extends String>)newValue);
742                                    return;
743                            case ConfigPackage.NAMED_MAP__SUPPORTED_EXECUTION_ENVIRONMENTS:
744                                    getSupportedExecutionEnvironments().clear();
745                                    getSupportedExecutionEnvironments().addAll((Collection<? extends String>)newValue);
746                                    return;
747                            case ConfigPackage.NAMED_MAP__HOME_PAGE:
748                                    setHomePage((String)newValue);
749                                    return;
750                            case ConfigPackage.NAMED_MAP__NAME:
751                                    setName((String)newValue);
752                                    return;
753                            case ConfigPackage.NAMED_MAP__RUNTIME:
754                                    setRuntime((Boolean)newValue);
755                                    return;
756                            case ConfigPackage.NAMED_MAP__ENTRY:
757                                    getEntry().clear();
758                                    getEntry().addAll((Collection<? extends MapEntry>)newValue);
759                                    return;
760                            case ConfigPackage.NAMED_MAP__TYPE:
761                                    setType((String)newValue);
762                                    return;
763                            case ConfigPackage.NAMED_MAP__FACTORY_MAP:
764                                    setFactoryMap((Boolean)newValue);
765                                    return;
766                    }
767                    super.eSet(featureID, newValue);
768            }
769    
770            /**
771             * <!-- begin-user-doc -->
772             * <!-- end-user-doc -->
773             * @generated
774             */
775            @Override
776            public void eUnset(int featureID) {
777                    switch (featureID) {
778                            case ConfigPackage.NAMED_MAP__ROLES:
779                                    getRoles().clear();
780                                    return;
781                            case ConfigPackage.NAMED_MAP__DESCRIPTION:
782                                    setDescription(DESCRIPTION_EDEFAULT);
783                                    return;
784                            case ConfigPackage.NAMED_MAP__CONFIG_SCRIPT:
785                                    setConfigScript(CONFIG_SCRIPT_EDEFAULT);
786                                    return;
787                            case ConfigPackage.NAMED_MAP__ID:
788                                    setId(ID_EDEFAULT);
789                                    return;
790                            case ConfigPackage.NAMED_MAP__SOURCE:
791                                    getSource().clear();
792                                    return;
793                            case ConfigPackage.NAMED_MAP__ENABLED:
794                                    setEnabled(ENABLED_EDEFAULT);
795                                    return;
796                            case ConfigPackage.NAMED_MAP__SERVICE:
797                                    getService().clear();
798                                    return;
799                            case ConfigPackage.NAMED_MAP__SUPPORTED_EXECUTION_ENVIRONMENTS:
800                                    getSupportedExecutionEnvironments().clear();
801                                    return;
802                            case ConfigPackage.NAMED_MAP__HOME_PAGE:
803                                    setHomePage(HOME_PAGE_EDEFAULT);
804                                    return;
805                            case ConfigPackage.NAMED_MAP__NAME:
806                                    setName(NAME_EDEFAULT);
807                                    return;
808                            case ConfigPackage.NAMED_MAP__RUNTIME:
809                                    setRuntime(RUNTIME_EDEFAULT);
810                                    return;
811                            case ConfigPackage.NAMED_MAP__ENTRY:
812                                    getEntry().clear();
813                                    return;
814                            case ConfigPackage.NAMED_MAP__TYPE:
815                                    setType(TYPE_EDEFAULT);
816                                    return;
817                            case ConfigPackage.NAMED_MAP__FACTORY_MAP:
818                                    setFactoryMap(FACTORY_MAP_EDEFAULT);
819                                    return;
820                    }
821                    super.eUnset(featureID);
822            }
823    
824            /**
825             * <!-- begin-user-doc -->
826             * <!-- end-user-doc -->
827             * @generated
828             */
829            @Override
830            public boolean eIsSet(int featureID) {
831                    switch (featureID) {
832                            case ConfigPackage.NAMED_MAP__ROLES:
833                                    return roles != null && !roles.isEmpty();
834                            case ConfigPackage.NAMED_MAP__DESCRIPTION:
835                                    return DESCRIPTION_EDEFAULT == null ? description != null : !DESCRIPTION_EDEFAULT.equals(description);
836                            case ConfigPackage.NAMED_MAP__CONFIG_SCRIPT:
837                                    return CONFIG_SCRIPT_EDEFAULT == null ? configScript != null : !CONFIG_SCRIPT_EDEFAULT.equals(configScript);
838                            case ConfigPackage.NAMED_MAP__ID:
839                                    return ID_EDEFAULT == null ? id != null : !ID_EDEFAULT.equals(id);
840                            case ConfigPackage.NAMED_MAP__SOURCE:
841                                    return source != null && !source.isEmpty();
842                            case ConfigPackage.NAMED_MAP__ENABLED:
843                                    return enabled != ENABLED_EDEFAULT;
844                            case ConfigPackage.NAMED_MAP__SERVICE:
845                                    return service != null && !service.isEmpty();
846                            case ConfigPackage.NAMED_MAP__SUPPORTED_EXECUTION_ENVIRONMENTS:
847                                    return supportedExecutionEnvironments != null && !supportedExecutionEnvironments.isEmpty();
848                            case ConfigPackage.NAMED_MAP__HOME_PAGE:
849                                    return HOME_PAGE_EDEFAULT == null ? homePage != null : !HOME_PAGE_EDEFAULT.equals(homePage);
850                            case ConfigPackage.NAMED_MAP__NAME:
851                                    return NAME_EDEFAULT == null ? name != null : !NAME_EDEFAULT.equals(name);
852                            case ConfigPackage.NAMED_MAP__RUNTIME:
853                                    return runtime != RUNTIME_EDEFAULT;
854                            case ConfigPackage.NAMED_MAP__ENTRY:
855                                    return entry != null && !entry.isEmpty();
856                            case ConfigPackage.NAMED_MAP__TYPE:
857                                    return TYPE_EDEFAULT == null ? type != null : !TYPE_EDEFAULT.equals(type);
858                            case ConfigPackage.NAMED_MAP__FACTORY_MAP:
859                                    return factoryMap != FACTORY_MAP_EDEFAULT;
860                    }
861                    return super.eIsSet(featureID);
862            }
863    
864            /**
865             * <!-- begin-user-doc -->
866             * <!-- end-user-doc -->
867             * @generated
868             */
869            @Override
870            public int eBaseStructuralFeatureID(int derivedFeatureID, Class<?> baseClass) {
871                    if (baseClass == Map.class) {
872                            switch (derivedFeatureID) {
873                                    case ConfigPackage.NAMED_MAP__ENTRY: return ConfigPackage.MAP__ENTRY;
874                                    case ConfigPackage.NAMED_MAP__TYPE: return ConfigPackage.MAP__TYPE;
875                                    case ConfigPackage.NAMED_MAP__FACTORY_MAP: return ConfigPackage.MAP__FACTORY_MAP;
876                                    default: return -1;
877                            }
878                    }
879                    return super.eBaseStructuralFeatureID(derivedFeatureID, baseClass);
880            }
881    
882            /**
883             * <!-- begin-user-doc -->
884             * <!-- end-user-doc -->
885             * @generated
886             */
887            @Override
888            public int eDerivedStructuralFeatureID(int baseFeatureID, Class<?> baseClass) {
889                    if (baseClass == Map.class) {
890                            switch (baseFeatureID) {
891                                    case ConfigPackage.MAP__ENTRY: return ConfigPackage.NAMED_MAP__ENTRY;
892                                    case ConfigPackage.MAP__TYPE: return ConfigPackage.NAMED_MAP__TYPE;
893                                    case ConfigPackage.MAP__FACTORY_MAP: return ConfigPackage.NAMED_MAP__FACTORY_MAP;
894                                    default: return -1;
895                            }
896                    }
897                    return super.eDerivedStructuralFeatureID(baseFeatureID, baseClass);
898            }
899    
900            /**
901             * <!-- begin-user-doc -->
902             * <!-- end-user-doc -->
903             * @generated
904             */
905            @Override
906            public String toString() {
907                    if (eIsProxy()) return super.toString();
908    
909                    StringBuffer result = new StringBuffer(super.toString());
910                    result.append(" (description: ");
911                    result.append(description);
912                    result.append(", configScript: ");
913                    result.append(configScript);
914                    result.append(", id: ");
915                    result.append(id);
916                    result.append(", enabled: ");
917                    result.append(enabled);
918                    result.append(", service: ");
919                    result.append(service);
920                    result.append(", supportedExecutionEnvironments: ");
921                    result.append(supportedExecutionEnvironments);
922                    result.append(", homePage: ");
923                    result.append(homePage);
924                    result.append(", name: ");
925                    result.append(name);
926                    result.append(", runtime: ");
927                    result.append(runtime);
928                    result.append(", type: ");
929                    result.append(type);
930                    result.append(", factoryMap: ");
931                    result.append(factoryMap);
932                    result.append(')');
933                    return result.toString();
934            }
935            
936            @SuppressWarnings("unchecked")
937            public void inject(final InjectionConfig injectionConfig) throws ConfigurationException {
938                    logger.fine("Injecting");
939                    ClassLoader cl = injectionConfig.getClassLoader();
940                    if (cl==null) {
941                            cl = getClass().getClassLoader();
942                    }
943                    final ClassLoader theClassLoader = cl;
944                    
945                    final java.util.Map[] createdInstance = {null};
946                    if (getType()==null || getType().trim().length()==0) {
947                            // Look for createXXX method
948                            createdInstance[0] = (java.util.Map<Object, Object>) NamedObjectDefinitionImpl.create(injectionConfig.getTarget(), getName());
949                            if (createdInstance[0]==null) {
950                                    throw new ConfigurationException("Could not create collection for property "+getName());
951                            }
952                    }
953                    
954                    FactoryClosure<Object> result = new FactoryClosureBase<Object>() {
955    
956                            public Object create(Object... args) throws ConfigurationException {
957                                    try {
958                                            java.util.Map<Object, Object> map = createdInstance[0];
959                                            if (map==null) {
960                                                    map = (java.util.Map<Object, Object>) ObjectDefinitionImpl.construct(theClassLoader.loadClass(getType()), args);
961                                            } else if (args.length>0) {
962                                                    throw new ConfigurationException("Invocation of create() with arguments is not compatible with createXXX()");
963                                            }
964                                            
965                                            InjectionConfig newInjectionConfig = (InjectionConfig) injectionConfig.clone();
966                                            newInjectionConfig.addToObjectPath(map);
967                                            
968                                            for (MapEntry entry: getEntry()) {
969                                                    FactoryClosure<Object> kfr = entry.getKey().create(newInjectionConfig);
970                                                    FactoryClosure<Object> vfr = entry.getValue().create(newInjectionConfig);                         
971                                                    map.put(kfr.create(), isFactoryMap() ? vfr : vfr.create());
972                                                    destroyableSink.addDestroyable(kfr.getDestroyable());
973                                                    destroyableSink.addDestroyable(vfr.getDestroyable());
974                                            }
975                                            
976                                            NamedMap def = NamedMapImpl.this;
977                                            // Init.
978                                            if (map instanceof Component) {
979                                                    Component<Map> comp = (Component<Map>) map;
980                                                    ConfigurationContext<Map> context = (ConfigurationContext<Map>) injectionConfig.getContextFactory().createContext(def, comp, injectionConfig, destroyableSink); 
981                                                    comp.init(context);
982                                            }
983                                            
984                                            // Execute script once all keys and values are initialized
985                                            if (getConfigScript()!=null && getConfigScript().trim().length()>0) {
986                                                    ScriptEvaluator se = new ScriptEvaluator();
987                                                    se.setParameters(new String[] {"instance", "definition", "config"}, new Class[] {map.getClass(), def.getClass(), injectionConfig.getClass()});
988                                                    se.setParentClassLoader(theClassLoader);
989                                                    se.setThrownExceptions(new Class[] {ConfigurationException.class});
990                                                    se.cook(getConfigScript());
991                                                    se.evaluate(new Object[] {map, def, injectionConfig});
992                                            }
993                                            
994                                            ObjectDefinitionImpl.expose(NamedMapImpl.this, map, injectionConfig.getContext(), theClassLoader, destroyableSink);
995                                            toDestroy(map);
996                                            return map;
997                                    } catch (ConfigurationException e) {
998                                            throw e;
999                                    } catch (Exception e) {
1000                                            throw new ConfigurationException(e);
1001                                    }
1002                            }
1003                            
1004                    };
1005    
1006                    injectionConfig.getDestroyableSink().addDestroyable(result.getDestroyable());
1007                    
1008                    if (createdInstance[0]==null) {
1009                            NamedObjectDefinitionImpl.inject(getName(), result, injectionConfig.getTarget());
1010                    } else {
1011                            result.create(); // Force initialization.
1012                    }
1013                    
1014            }       
1015    
1016    } //NamedMapImpl