/** convenience method that wires the object for a given descriptor. */ public static Object create(final Descriptor descriptor) { final WireContext wireContext = new WireContext(); return wireContext.create(descriptor, false); }
void initialize(final Object object, final Descriptor descriptor, final boolean isDelayable) { final InitializationType initializationType = getInitializationType(object, descriptor, isDelayable); if (initializationType == InitializationType.IMMEDIATE) { performInitialization(object, descriptor); } else if (initializationType == InitializationType.DELAYEBLE) { addPendingInitialization(object, descriptor); } else { final String objectName = descriptor.getName(); if (objectName != null) { set(objectName, object); } } }
/** * creates a new object for the given descriptor. * * @param isDelayable * indicates wether initialization is delayable. When isDelayable is set to false the returned object will be * constructed and initialized. When isDelayable is set to true, the returned object will be constructed, but * not necessarily initialized. */ public Object create(final Descriptor descriptor, final boolean isDelayable) { Object object = null; object = construct(descriptor); initialize(object, descriptor, isDelayable); processPendingInitializations(); return object; }
if (hasException(objectName)) { final String message = ExceptionManager.getInstance().getFullMessage("bp_WC_2", objectName); throw new WireException(message, exceptions.get(objectName)); if (hasCached(objectName)) { final Object object = cache.get(objectName); if (LOG.isLoggable(Level.FINE)) { final Object constructed = getConstructed(objectName); if (isDelayable && null != constructed) { final Object object = constructed; if (isUnderConstruction(objectName) || isUnderInitialization(objectName)) { final String message = ExceptionManager.getInstance().getFullMessage("bp_WC_3", objectName); throw new WireException(message); return create(objectName, isDelayable);
/** * Initializes all the eager objects defined in the {@link #wireDefinition}. */ void initializeEagerObjects() { if (wireDefinition != null) { final List<String> eagerInitObjectNames = wireDefinition.getEagerInitNames(); if (eagerInitObjectNames != null) { for (final String eagerInitObjectName : eagerInitObjectNames) { final Descriptor descriptor = wireDefinition.getDescriptor(eagerInitObjectName); if (descriptor.isEagerInit()) { if (LOG.isLoggable(Level.FINE)) { LOG.fine("eagerly initializing " + eagerInitObjectName); } get(eagerInitObjectName, descriptor.isDelayable()); } } while (!hasObjectUnderConstruction() && !hasObjectUnderInitialization() && hasPendingInitializations()) { processPendingInitializations(); } } } }
void performInitialization(final Object object, final Descriptor descriptor) { final String objectName = descriptor.getName(); if (objectName != null) { fireObjectEvent(Descriptor.EVENT_INITIALIZING, objectName, object); if (underInitialization == null) { underInitialization = new HashMap<String, Object>(); } underInitialization.put(objectName, object); if (LOG.isLoggable(Level.FINE)) { LOG.fine("initializing " + objectName); } } try { descriptor.initialize(object, this); } catch (final RuntimeException e) { addException(descriptor, e); throw e; } if (objectName != null) { underInitialization.remove(objectName); // event constructed is fired before the object is put in the cache // because that generates a set event fireObjectEvent(Descriptor.EVENT_CONSTRUCTED, objectName, object); set(objectName, object); } }
@Override public Object construct(final WireContext wireContext) { Configuration configuration = null; if (configurationName != null) { configuration = (Configuration) wireContext.get(configurationName); } else if (configurationDescriptor != null) { configuration = (Configuration) wireContext.create(configurationDescriptor, false); } else { configuration = wireContext.get(Configuration.class); } if (configuration == null) { final String message = ExceptionManager.getInstance().getFullMessage("bp_HSF_1"); throw new WireException(message); } SearchUtil.addSearchConfiguration(configuration); addInterceptor(configuration); final SessionFactory sessionFactory = configuration.buildSessionFactory(); wireContext.addListener(new SessionFactoryCloser(sessionFactory)); return sessionFactory; }
@Override public Environment openEnvironment() { final PvmEnvironment environment = new PvmEnvironment(this); if (LOG.isLoggable(Level.FINE)) { LOG.fine("opening " + environment); } // set the classloader final ClassLoader classLoader = environmentFactoryCtxWireContext.getClassLoader(); if (classLoader != null) { environment.setClassLoader(classLoader); } // add the environment-factory context environment.addContext(environmentFactoryCtxWireContext); // add the environment block context final WireContext environmentContext = new WireContext(environmentCtxWireDefinition, Context.CONTEXTNAME_ENVIRONMENT, true); // add the environment block context to the environment environment.addContext(environmentContext); try { // finish the creation of the environment wire context environmentContext.create(); } catch (final RuntimeException e) { // On exception, pop environment Environment.popEnvironment(); throw e; } // if all went well, return the created environment return environment; }
WireDefinition wireDefinition = wireContext.getWireDefinition(); Object object = wireContext.get(objectName); if (object == null) { String message = ExceptionManager.getInstance().getFullMessage( "bp_SO_5", wireContext.getName(), objectName); throw new WireException(message); "bp_SO_6", wireContext.getName(), objectName, object.getClass().getName(), Observable.class.getName()); throw new WireException(message);
ClassLoader classLoader = wireContext.getClassLoader(); clazz = ReflectUtil.loadClass(classLoader, className); } catch (Exception e) { object = wireContext.get(factoryObjectName, false); if (object == null) { String message = ExceptionManager.getInstance().getFullMessage( object = wireContext.create(factoryDescriptor, false); if (object == null) { String message = ExceptionManager.getInstance().getFullMessage(
@Override public <T> T get(final Class<T> type) { return environmentFactoryCtxWireContext.get(type); }
/** * when this {@link Context} is used in an {@link Environment}, it needs a name. */ public WireContext(final WireDefinition wireDefinition, final String name) { super(); this.wireDefinition = wireDefinition; this.name = name; classLoader = wireDefinition != null ? wireDefinition.getClassLoader() : null; create(); }
public Object construct(WireContext wireContext) { return wireContext.getClassLoader(); }
/** * initializes the eager objects and then fires the create event. This method only needs to be called explicitly in * case <code>delayCreate</code> is true in {@link #WireContext(WireDefinition, String, Environment, boolean)}. */ public void create() { if (LOG.isLoggable(Level.FINE)) { LOG.fine("creating " + name); } initializeEagerObjects(); fire(EVENT_OPEN, null); }
Object construct(final Descriptor descriptor) { Object object; final String objectName = descriptor.getName(); if (objectName != null) { fireObjectEvent(Descriptor.EVENT_CONSTRUCTING, objectName, null); if (underConstruction == null) { underConstruction = new HashSet<String>(); } underConstruction.add(objectName); if (LOG.isLoggable(Level.FINE)) { LOG.fine("constructing " + objectName); } } try { object = descriptor.construct(this); } catch (final RuntimeException e) { addException(descriptor, e); throw e; } if (objectName != null) { underConstruction.remove(objectName); } return object; }
@Override public Object parseDocument(final Document document, final Parse parse) { final Element documentElement = document.getDocumentElement(); // if the default environment factory was already set in the parse PvmEnvironmentFactory pvmEnvironmentFactory = (PvmEnvironmentFactory) parse.getDocumentObject(); if (pvmEnvironmentFactory == null) { pvmEnvironmentFactory = new PvmEnvironmentFactory(); parse.setDocumentObject(pvmEnvironmentFactory); } final WireDefinition environmentFactoryWireDefinition = getApplicationWireDefinition(documentElement, parse); final WireDefinition environmentWireDefinition = getBlockWireDefinition(documentElement, parse); // create the application wire context from the definition final WireContext environmentFactoryWireContext = new WireContext(environmentFactoryWireDefinition, Context.CONTEXTNAME_ENVIRONMENT_FACTORY); // propagate the parser classloader to the application context environmentFactoryWireContext.setClassLoader(classLoader); // configure the default environment factory pvmEnvironmentFactory.setEnvironmentFactoryCtxWireContext(environmentFactoryWireContext); pvmEnvironmentFactory.setEnvironmentCtxWireDefinition(environmentWireDefinition); parse.setDocumentObject(pvmEnvironmentFactory); return pvmEnvironmentFactory; }
@Override public void close() { environmentFactoryCtxWireContext.fire(WireContext.EVENT_CLOSE, null); }
/** * removes an object from the context and fires the remove event. * * @return previous object associated with the given name, or null if there was no mapping for this name. */ public Object remove(final String objectName) { Object removed = null; if (cache != null) { removed = cache.remove(objectName); fireObjectEvent(Descriptor.EVENT_REMOVE, objectName, removed); } return removed; }
public Object construct(WireContext wireContext) { EventExecutor eventExecutor = (EventExecutor) super.construct(wireContext); if (autoStart) { wireContext.addListener(new EventExecutorStopper(eventExecutor)); } return eventExecutor; }