use of javax.naming.Reference in project kernel by exoplatform.
the class InitialContextBinder method saveBindings.
/**
* Export references into xml-file.
*
* @throws XMLStreamException
* if any exception occurs during export
* @throws FileNotFoundException
* if can't open output stream from file
*/
protected synchronized void saveBindings() throws FileNotFoundException, XMLStreamException {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = outputFactory.createXMLStreamWriter(PrivilegedFileHelper.fileOutputStream(bindingsStorePath), "UTF-8");
writer.writeStartDocument("UTF-8", "1.0");
writer.writeStartElement(BIND_REFERENCES_ELEMENT);
for (Entry<String, Reference> entry : bindings.entrySet()) {
String bindName = entry.getKey();
Reference reference = entry.getValue();
writer.writeStartElement(REFERENCE_ELEMENT);
writer.writeAttribute(BIND_NAME_ATTR, bindName);
if (reference.getClassName() != null) {
writer.writeAttribute(CLASS_NAME_ATTR, reference.getClassName());
}
if (reference.getFactoryClassName() != null) {
writer.writeAttribute(FACTORY_ATTR, reference.getFactoryClassName());
}
if (reference.getFactoryClassLocation() != null) {
writer.writeAttribute(FACTORY_LOCATION_ATTR, reference.getFactoryClassLocation());
}
writer.writeStartElement(REFADDR_ELEMENT);
for (int i = 0; i < reference.size(); i++) {
writer.writeStartElement(PROPERTY_ELEMENT);
writer.writeAttribute(reference.get(i).getType(), (String) reference.get(i).getContent());
writer.writeEndElement();
}
writer.writeEndElement();
writer.writeEndElement();
}
writer.writeEndElement();
writer.writeEndDocument();
}
use of javax.naming.Reference in project kernel by exoplatform.
the class InitialContextBinder method readBindings.
/**
* Import references from xml-file.
*
* @return map with bind name - references
*
* @throws XMLStreamException
* if errors occurs during import
* @throws FileNotFoundException
* if can't open input stream from file
*/
protected Map<String, Reference> readBindings() throws FileNotFoundException, XMLStreamException {
Stack<RefEntity> stack = new Stack<RefEntity>();
Map<String, Reference> importedRefs = new HashMap<String, Reference>();
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader reader = factory.createXMLEventReader(PrivilegedFileHelper.fileInputStream(bindingsStorePath), "UTF-8");
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
switch(event.getEventType()) {
case XMLStreamConstants.START_ELEMENT:
StartElement startElement = event.asStartElement();
Map<String, String> attr = new HashMap<String, String>();
Iterator attributes = startElement.getAttributes();
while (attributes.hasNext()) {
Attribute attribute = (Attribute) attributes.next();
attr.put(attribute.getName().getLocalPart(), attribute.getValue());
}
String localName = startElement.getName().getLocalPart();
if (localName.equals(REFERENCE_ELEMENT)) {
String bindName = attr.get(BIND_NAME_ATTR);
String className = attr.get(CLASS_NAME_ATTR);
String factoryName = attr.get(FACTORY_ATTR);
String factoryLocation = attr.get(FACTORY_LOCATION_ATTR);
Reference reference = new Reference(className, factoryName, factoryLocation);
stack.push(new RefEntity(bindName, reference));
} else if (localName.equals(PROPERTY_ELEMENT)) {
RefEntity refEntity = stack.pop();
Reference reference = refEntity.getValue();
for (Entry<String, String> entry : attr.entrySet()) {
reference.add(new StringRefAddr(entry.getKey(), entry.getValue()));
}
refEntity.setValue(reference);
stack.push(refEntity);
}
break;
case XMLStreamConstants.END_ELEMENT:
EndElement endElement = event.asEndElement();
localName = endElement.getName().getLocalPart();
if (localName.equals(REFERENCE_ELEMENT)) {
RefEntity refEntity = stack.pop();
importedRefs.put(refEntity.getKey(), refEntity.getValue());
}
break;
default:
break;
}
}
return importedRefs;
}
use of javax.naming.Reference in project cxf by apache.
the class ConnectionFactoryImplTest method testInstanceOfReferencable.
@Test
public void testInstanceOfReferencable() throws Exception {
assertTrue("Instance of Referenceable", cf instanceof Referenceable);
assertNull("No ref set", cf.getReference());
Reference ref = EasyMock.createMock(Reference.class);
cf.setReference(ref);
assertEquals("Got back what was set", ref, cf.getReference());
}
use of javax.naming.Reference in project Payara by payara.
the class NamingContext method bind.
/**
* Binds a name to an object. All intermediate contexts and the target
* context (that named by all but terminal atomic component of the name)
* must already exist.
*
* @param name the name to bind; may not be empty
* @param obj the object to bind; possibly null
* @param rebind if true, then perform a rebind (ie, overwrite)
* @exception NameAlreadyBoundException if name is already bound
* @exception InvalidAttributesException if object did not supply all
* mandatory attributes
* @exception NamingException if a naming exception is encountered
*/
protected void bind(Name name, Object obj, boolean rebind) throws NamingException {
checkWritable();
while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1);
if (name.isEmpty())
throw new NamingException(rb.getString(LogFacade.INVALID_NAME));
NamingEntry entry = (NamingEntry) bindings.get(name.get(0));
if (name.size() > 1) {
if (entry == null) {
throw new NameNotFoundException(MessageFormat.format(rb.getString(LogFacade.NAME_NOT_BOUND), name.get(0)));
}
if (entry.type == NamingEntry.CONTEXT) {
if (rebind) {
((Context) entry.value).rebind(name.getSuffix(1), obj);
} else {
((Context) entry.value).bind(name.getSuffix(1), obj);
}
} else {
throw new NamingException(rb.getString(LogFacade.CONTEXT_EXPECTED));
}
} else {
if ((!rebind) && (entry != null)) {
throw new NamingException(MessageFormat.format(rb.getString(LogFacade.ALREADY_BOUND), name.get(0)));
} else {
// Getting the type of the object and wrapping it within a new
// NamingEntry
Object toBind = NamingManager.getStateToBind(obj, name, this, env);
if (toBind instanceof Context) {
entry = new NamingEntry(name.get(0), toBind, NamingEntry.CONTEXT);
} else if (toBind instanceof LinkRef) {
entry = new NamingEntry(name.get(0), toBind, NamingEntry.LINK_REF);
} else if (toBind instanceof Reference) {
entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE);
} else if (toBind instanceof Referenceable) {
toBind = ((Referenceable) toBind).getReference();
entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE);
} else {
entry = new NamingEntry(name.get(0), toBind, NamingEntry.ENTRY);
}
bindings.put(name.get(0), entry);
}
}
}
use of javax.naming.Reference in project Payara by payara.
the class EjbFactory method getObjectInstance.
// ----------------------------------------------------------- Constructors
// -------------------------------------------------------------- Constants
// ----------------------------------------------------- Instance Variables
// --------------------------------------------------------- Public Methods
// -------------------------------------------------- ObjectFactory Methods
/**
* Crete a new EJB instance.
*
* @param obj The reference object describing the DataSource
*/
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
if (obj instanceof EjbRef) {
Reference ref = (Reference) obj;
// If ejb-link has been specified, resolving the link using JNDI
RefAddr linkRefAddr = ref.get(EjbRef.LINK);
if (linkRefAddr != null) {
// Retrieving the EJB link
String ejbLink = linkRefAddr.getContent().toString();
Object beanObj = (new InitialContext()).lookup(ejbLink);
/*
String homeClassName = ref.getClassName();
try {
Class home = Class.forName(homeClassName);
if (home.isInstance(beanObj)) {
if (log.isDebugEnabled())
log.debug("Bean of type "
+ beanObj.getClass().getName()
+ " implements home interface "
+ home.getName());
} else {
if (log.isDebugEnabled())
log.debug("Bean of type "
+ beanObj.getClass().getName()
+ " doesn't implement home interface "
+ home.getName());
throw new NamingException
("Bean of type " + beanObj.getClass().getName()
+ " doesn't implement home interface "
+ home.getName());
}
} catch (ClassNotFoundException e) {
log.warn("Couldn't load home interface "
+ homeClassName, e);
}
*/
return beanObj;
}
ObjectFactory factory = null;
RefAddr factoryRefAddr = ref.get(Constants.FACTORY);
if (factoryRefAddr != null) {
// Using the specified factory
String factoryClassName = factoryRefAddr.getContent().toString();
// Loading factory
ClassLoader tcl = Thread.currentThread().getContextClassLoader();
Class<?> factoryClass = null;
if (tcl != null) {
try {
factoryClass = tcl.loadClass(factoryClassName);
} catch (ClassNotFoundException e) {
}
} else {
try {
factoryClass = Class.forName(factoryClassName);
} catch (ClassNotFoundException e) {
}
}
if (factoryClass != null) {
try {
factory = (ObjectFactory) factoryClass.newInstance();
} catch (Throwable t) {
}
}
} else {
String javaxEjbFactoryClassName = System.getProperty("javax.ejb.Factory", Constants.OPENEJB_EJB_FACTORY);
try {
factory = (ObjectFactory) Class.forName(javaxEjbFactoryClassName).newInstance();
} catch (Throwable t) {
}
}
if (factory != null) {
return factory.getObjectInstance(obj, name, nameCtx, environment);
} else {
throw new NamingException("Cannot create resource instance");
}
}
return null;
}
Aggregations