/** * 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(); }
/** * creates a new object for the given objectName as defined in the {@link #wireDefinition}. * * @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. */ protected Object create(final String objectName, final boolean isDelayable) { final Descriptor descriptor = wireDefinition.getDescriptor(objectName); return create(descriptor, isDelayable); }
/** 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); }
/** * allows for postponing the creation of this wire context. * * @param delayCreate * specifies if creation should be postponed till {@link #create()} is called explicitly. If delayCreate is * set to false, creation is done as part of the constructor. If delayCreate is set to true, the * {@link #create()} method needs to be called explicitly by the client after construction is complete. The * use case is creation of environment where the transactionName needs to be set and the scope needs to be * added to the environment before the creation of this wire scope is done. * @see PvmEnvironmentFactory#openEnvironment() */ public WireContext(final WireDefinition wireDefinition, final String name, final boolean delayCreate) { super(); this.wireDefinition = wireDefinition; this.name = name; classLoader = wireDefinition != null ? wireDefinition.getClassLoader() : null; if (!delayCreate) { create(); } }
@SuppressWarnings("unchecked") public void initialize(Object object, WireContext wireContext) { Map<Object, Object> map = (Map<Object, Object>) object; try { if (keyDescriptors != null) { for (int i = 0; i < keyDescriptors.size(); i++) { Descriptor keyDescriptor = keyDescriptors.get(i); Descriptor valueDescriptor = valueDescriptors.get(i); Object key = wireContext.create(keyDescriptor, true); Object value = wireContext.create(valueDescriptor, true); map.put(key, value); } } } catch (WireException e) { throw e; } catch (Exception e) { String message = ExceptionManager.getInstance().getFullMessage( "bp_MD_1", (name != null ? name : className)); throw new WireException(message, e); } }
@Override @SuppressWarnings("unchecked") public void initialize(final Object object, final WireContext wireContext) { final Collection<Object> collection = (Collection<Object>) object; try { if (valueDescriptors != null) { for (final Descriptor descriptor : valueDescriptors) { final Object element = wireContext.create(descriptor, true); if (LOG.isLoggable(Level.FINE)) { LOG.fine("adding element " + element + " to collection"); } collection.add(element); } } } catch (final WireException e) { throw e; } catch (final Exception e) { final String message = ExceptionManager.getInstance().getFullMessage("bp_CollD_2", name != null ? name : className); throw new WireException(message, e); } }
Object arg; try { arg = wireContext.create(argDescriptor.getDescriptor(), true); args[i] = arg; } catch (RuntimeException e) {
@Override public void initialize(final Object object, final WireContext wireContext) { final Configuration configuration = (Configuration) object; apply(mappingOperations, configuration, wireContext); apply(cacheOperations, configuration, wireContext); if (propertiesDescriptor != null) { final Properties properties = (Properties) wireContext.create(propertiesDescriptor, false); if (LOG.isLoggable(Level.FINE)) { LOG.fine("adding properties to hibernate configuration: " + properties); } configuration.addProperties(properties); } if (schemaOperation != null) { schemaOperation.apply(configuration, wireContext); } }
public void apply(Object target, WireContext wireContext) { // create the value to assign to the property Object value = wireContext.create(descriptor, true); Method method = null; Class<?> clazz = target.getClass(); Object[] args = new Object[] { value }; method = ReflectUtilDescriptor.findMethod(clazz, setterName, null, args); if (method == null) { String message = ExceptionManager.getInstance().getFullMessage( "bp_PO_1", setterName, value); throw new WireException(message); } ReflectUtil.invoke(method, target, args); }
@Override public void apply(final Object target, final WireContext wireContext) { if (target != null) { // Get field synchronized (this) { if (field == null) { final Class<?> clazz = target.getClass(); field = ReflectUtil.getField(clazz, fieldName); } } // Create value final Object value = wireContext.create(descriptor, true); // Set the field value try { ReflectUtil.set(field, target, value); } catch (final Exception e) { final String message = ExceptionManager.getInstance().getFullMessage("bp_FO_1", fieldName, value); throw new WireException(message, e); } } }
return create(objectName, isDelayable);
@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; }
@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; }
object = wireContext.create(factoryDescriptor, false); if (object == null) { String message = ExceptionManager.getInstance().getFullMessage(