init
This commit is contained in:
309
java/org/apache/naming/factory/BeanFactory.java
Normal file
309
java/org/apache/naming/factory/BeanFactory.java
Normal file
@@ -0,0 +1,309 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.naming.factory;
|
||||
|
||||
import java.beans.BeanInfo;
|
||||
import java.beans.Introspector;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.Name;
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.RefAddr;
|
||||
import javax.naming.Reference;
|
||||
import javax.naming.spi.ObjectFactory;
|
||||
|
||||
import org.apache.naming.ResourceRef;
|
||||
|
||||
/**
|
||||
* Object factory for any Resource conforming to the JavaBean spec.
|
||||
*
|
||||
* <p>This factory can be configured in a <code><Context></code> element
|
||||
* in your <code>conf/server.xml</code>
|
||||
* configuration file. An example of factory configuration is:</p>
|
||||
* <pre>
|
||||
* <Resource name="jdbc/myDataSource" auth="SERVLET"
|
||||
* type="oracle.jdbc.pool.OracleConnectionCacheImpl"/>
|
||||
* <ResourceParams name="jdbc/myDataSource">
|
||||
* <parameter>
|
||||
* <name>factory</name>
|
||||
* <value>org.apache.naming.factory.BeanFactory</value>
|
||||
* </parameter>
|
||||
* <parameter>
|
||||
* <name>driverType</name>
|
||||
* <value>thin</value>
|
||||
* </parameter>
|
||||
* <parameter>
|
||||
* <name>serverName</name>
|
||||
* <value>hue</value>
|
||||
* </parameter>
|
||||
* <parameter>
|
||||
* <name>networkProtocol</name>
|
||||
* <value>tcp</value>
|
||||
* </parameter>
|
||||
* <parameter>
|
||||
* <name>databaseName</name>
|
||||
* <value>XXXX</value>
|
||||
* </parameter>
|
||||
* <parameter>
|
||||
* <name>portNumber</name>
|
||||
* <value>NNNN</value>
|
||||
* </parameter>
|
||||
* <parameter>
|
||||
* <name>user</name>
|
||||
* <value>XXXX</value>
|
||||
* </parameter>
|
||||
* <parameter>
|
||||
* <name>password</name>
|
||||
* <value>XXXX</value>
|
||||
* </parameter>
|
||||
* <parameter>
|
||||
* <name>maxLimit</name>
|
||||
* <value>5</value>
|
||||
* </parameter>
|
||||
* </ResourceParams>
|
||||
* </pre>
|
||||
*
|
||||
* @author Aner Perez [aner at ncstech.com]
|
||||
*/
|
||||
public class BeanFactory
|
||||
implements ObjectFactory {
|
||||
|
||||
// ----------------------------------------------------------- Constructors
|
||||
|
||||
|
||||
// -------------------------------------------------------------- Constants
|
||||
|
||||
|
||||
// ----------------------------------------------------- Instance Variables
|
||||
|
||||
|
||||
// --------------------------------------------------------- Public Methods
|
||||
|
||||
|
||||
// -------------------------------------------------- ObjectFactory Methods
|
||||
|
||||
|
||||
/**
|
||||
* Create a new Bean instance.
|
||||
*
|
||||
* @param obj The reference object describing the Bean
|
||||
*/
|
||||
@Override
|
||||
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
|
||||
Hashtable<?,?> environment)
|
||||
throws NamingException {
|
||||
|
||||
if (obj instanceof ResourceRef) {
|
||||
|
||||
try {
|
||||
|
||||
Reference ref = (Reference) obj;
|
||||
String beanClassName = ref.getClassName();
|
||||
Class<?> beanClass = null;
|
||||
ClassLoader tcl =
|
||||
Thread.currentThread().getContextClassLoader();
|
||||
if (tcl != null) {
|
||||
try {
|
||||
beanClass = tcl.loadClass(beanClassName);
|
||||
} catch(ClassNotFoundException e) {
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
beanClass = Class.forName(beanClassName);
|
||||
} catch(ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (beanClass == null) {
|
||||
throw new NamingException
|
||||
("Class not found: " + beanClassName);
|
||||
}
|
||||
|
||||
BeanInfo bi = Introspector.getBeanInfo(beanClass);
|
||||
PropertyDescriptor[] pda = bi.getPropertyDescriptors();
|
||||
|
||||
Object bean = beanClass.getConstructor().newInstance();
|
||||
|
||||
/* Look for properties with explicitly configured setter */
|
||||
RefAddr ra = ref.get("forceString");
|
||||
Map<String, Method> forced = new HashMap<>();
|
||||
String value;
|
||||
|
||||
if (ra != null) {
|
||||
value = (String)ra.getContent();
|
||||
Class<?> paramTypes[] = new Class[1];
|
||||
paramTypes[0] = String.class;
|
||||
String setterName;
|
||||
int index;
|
||||
|
||||
/* Items are given as comma separated list */
|
||||
for (String param: value.split(",")) {
|
||||
param = param.trim();
|
||||
/* A single item can either be of the form name=method
|
||||
* or just a property name (and we will use a standard
|
||||
* setter) */
|
||||
index = param.indexOf('=');
|
||||
if (index >= 0) {
|
||||
setterName = param.substring(index + 1).trim();
|
||||
param = param.substring(0, index).trim();
|
||||
} else {
|
||||
setterName = "set" +
|
||||
param.substring(0, 1).toUpperCase(Locale.ENGLISH) +
|
||||
param.substring(1);
|
||||
}
|
||||
try {
|
||||
forced.put(param,
|
||||
beanClass.getMethod(setterName, paramTypes));
|
||||
} catch (NoSuchMethodException|SecurityException ex) {
|
||||
throw new NamingException
|
||||
("Forced String setter " + setterName +
|
||||
" not found for property " + param);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Enumeration<RefAddr> e = ref.getAll();
|
||||
|
||||
while (e.hasMoreElements()) {
|
||||
|
||||
ra = e.nextElement();
|
||||
String propName = ra.getType();
|
||||
|
||||
if (propName.equals(Constants.FACTORY) ||
|
||||
propName.equals("scope") || propName.equals("auth") ||
|
||||
propName.equals("forceString") ||
|
||||
propName.equals("singleton")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
value = (String)ra.getContent();
|
||||
|
||||
Object[] valueArray = new Object[1];
|
||||
|
||||
/* Shortcut for properties with explicitly configured setter */
|
||||
Method method = forced.get(propName);
|
||||
if (method != null) {
|
||||
valueArray[0] = value;
|
||||
try {
|
||||
method.invoke(bean, valueArray);
|
||||
} catch (IllegalAccessException|
|
||||
IllegalArgumentException|
|
||||
InvocationTargetException ex) {
|
||||
throw new NamingException
|
||||
("Forced String setter " + method.getName() +
|
||||
" threw exception for property " + propName);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
for (i = 0; i<pda.length; i++) {
|
||||
|
||||
if (pda[i].getName().equals(propName)) {
|
||||
|
||||
Class<?> propType = pda[i].getPropertyType();
|
||||
|
||||
if (propType.equals(String.class)) {
|
||||
valueArray[0] = value;
|
||||
} else if (propType.equals(Character.class)
|
||||
|| propType.equals(char.class)) {
|
||||
valueArray[0] =
|
||||
Character.valueOf(value.charAt(0));
|
||||
} else if (propType.equals(Byte.class)
|
||||
|| propType.equals(byte.class)) {
|
||||
valueArray[0] = Byte.valueOf(value);
|
||||
} else if (propType.equals(Short.class)
|
||||
|| propType.equals(short.class)) {
|
||||
valueArray[0] = Short.valueOf(value);
|
||||
} else if (propType.equals(Integer.class)
|
||||
|| propType.equals(int.class)) {
|
||||
valueArray[0] = Integer.valueOf(value);
|
||||
} else if (propType.equals(Long.class)
|
||||
|| propType.equals(long.class)) {
|
||||
valueArray[0] = Long.valueOf(value);
|
||||
} else if (propType.equals(Float.class)
|
||||
|| propType.equals(float.class)) {
|
||||
valueArray[0] = Float.valueOf(value);
|
||||
} else if (propType.equals(Double.class)
|
||||
|| propType.equals(double.class)) {
|
||||
valueArray[0] = Double.valueOf(value);
|
||||
} else if (propType.equals(Boolean.class)
|
||||
|| propType.equals(boolean.class)) {
|
||||
valueArray[0] = Boolean.valueOf(value);
|
||||
} else {
|
||||
throw new NamingException
|
||||
("String conversion for property " + propName +
|
||||
" of type '" + propType.getName() +
|
||||
"' not available");
|
||||
}
|
||||
|
||||
Method setProp = pda[i].getWriteMethod();
|
||||
if (setProp != null) {
|
||||
setProp.invoke(bean, valueArray);
|
||||
} else {
|
||||
throw new NamingException
|
||||
("Write not allowed for property: "
|
||||
+ propName);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (i == pda.length) {
|
||||
throw new NamingException
|
||||
("No set method found for property: " + propName);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return bean;
|
||||
|
||||
} catch (java.beans.IntrospectionException ie) {
|
||||
NamingException ne = new NamingException(ie.getMessage());
|
||||
ne.setRootCause(ie);
|
||||
throw ne;
|
||||
} catch (java.lang.ReflectiveOperationException e) {
|
||||
Throwable cause = e.getCause();
|
||||
if (cause instanceof ThreadDeath) {
|
||||
throw (ThreadDeath) cause;
|
||||
}
|
||||
if (cause instanceof VirtualMachineError) {
|
||||
throw (VirtualMachineError) cause;
|
||||
}
|
||||
NamingException ne = new NamingException(e.getMessage());
|
||||
ne.setRootCause(e);
|
||||
throw ne;
|
||||
}
|
||||
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
48
java/org/apache/naming/factory/Constants.java
Normal file
48
java/org/apache/naming/factory/Constants.java
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.naming.factory;
|
||||
|
||||
/**
|
||||
* Static constants for this package.
|
||||
*/
|
||||
public final class Constants {
|
||||
|
||||
public static final String Package = "org.apache.naming.factory";
|
||||
|
||||
public static final String DEFAULT_RESOURCE_FACTORY = Package + ".ResourceFactory";
|
||||
|
||||
public static final String DEFAULT_RESOURCE_LINK_FACTORY = Package + ".ResourceLinkFactory";
|
||||
|
||||
public static final String DEFAULT_TRANSACTION_FACTORY = Package + ".TransactionFactory";
|
||||
|
||||
public static final String DEFAULT_RESOURCE_ENV_FACTORY = Package + ".ResourceEnvFactory";
|
||||
|
||||
public static final String DEFAULT_EJB_FACTORY = Package + ".EjbFactory";
|
||||
|
||||
public static final String DEFAULT_SERVICE_FACTORY = Package + ".webservices.ServiceRefFactory";
|
||||
|
||||
public static final String DEFAULT_HANDLER_FACTORY = Package + ".HandlerFactory";
|
||||
|
||||
public static final String DBCP_DATASOURCE_FACTORY =
|
||||
"org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory";
|
||||
|
||||
public static final String OPENEJB_EJB_FACTORY = Package + ".OpenEjbFactory";
|
||||
|
||||
public static final String DEFAULT_LOOKUP_JNDI_FACTORY = Package + ".LookupFactory";
|
||||
|
||||
public static final String FACTORY = "factory";
|
||||
}
|
||||
150
java/org/apache/naming/factory/DataSourceLinkFactory.java
Normal file
150
java/org/apache/naming/factory/DataSourceLinkFactory.java
Normal file
@@ -0,0 +1,150 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
package org.apache.naming.factory;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Hashtable;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.Name;
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.RefAddr;
|
||||
import javax.naming.Reference;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* <p>Object factory for resource links for shared data sources.</p>
|
||||
*
|
||||
*/
|
||||
public class DataSourceLinkFactory extends ResourceLinkFactory {
|
||||
|
||||
public static void setGlobalContext(Context newGlobalContext) {
|
||||
ResourceLinkFactory.setGlobalContext(newGlobalContext);
|
||||
}
|
||||
// ------------------------------------------------- ObjectFactory Methods
|
||||
|
||||
|
||||
/**
|
||||
* Create a new DataSource instance.
|
||||
*
|
||||
* @param obj The reference object describing the DataSource
|
||||
*/
|
||||
@Override
|
||||
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?,?> environment)
|
||||
throws NamingException {
|
||||
Object result = super.getObjectInstance(obj, name, nameCtx, environment);
|
||||
// Can we process this request?
|
||||
if (result!=null) {
|
||||
Reference ref = (Reference) obj;
|
||||
RefAddr userAttr = ref.get("username");
|
||||
RefAddr passAttr = ref.get("password");
|
||||
if (userAttr.getContent()!=null && passAttr.getContent()!=null) {
|
||||
result = wrapDataSource(result,userAttr.getContent().toString(), passAttr.getContent().toString());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected Object wrapDataSource(Object datasource, String username, String password) throws NamingException {
|
||||
try {
|
||||
DataSourceHandler handler =
|
||||
new DataSourceHandler((DataSource)datasource, username, password);
|
||||
return Proxy.newProxyInstance(datasource.getClass().getClassLoader(),
|
||||
datasource.getClass().getInterfaces(), handler);
|
||||
}catch (Exception x) {
|
||||
if (x instanceof InvocationTargetException) {
|
||||
Throwable cause = x.getCause();
|
||||
if (cause instanceof ThreadDeath) {
|
||||
throw (ThreadDeath) cause;
|
||||
}
|
||||
if (cause instanceof VirtualMachineError) {
|
||||
throw (VirtualMachineError) cause;
|
||||
}
|
||||
if (cause instanceof Exception) {
|
||||
x = (Exception) cause;
|
||||
}
|
||||
}
|
||||
if (x instanceof NamingException) throw (NamingException)x;
|
||||
else {
|
||||
NamingException nx = new NamingException(x.getMessage());
|
||||
nx.initCause(x);
|
||||
throw nx;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple wrapper class that will allow a user to configure a ResourceLink for a data source
|
||||
* so that when {@link javax.sql.DataSource#getConnection()} is called, it will invoke
|
||||
* {@link javax.sql.DataSource#getConnection(String, String)} with the preconfigured username and password.
|
||||
*/
|
||||
public static class DataSourceHandler implements InvocationHandler {
|
||||
private final DataSource ds;
|
||||
private final String username;
|
||||
private final String password;
|
||||
private final Method getConnection;
|
||||
public DataSourceHandler(DataSource ds, String username, String password) throws Exception {
|
||||
this.ds = ds;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
getConnection = ds.getClass().getMethod("getConnection", String.class, String.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
|
||||
if ("getConnection".equals(method.getName()) && (args==null || args.length==0)) {
|
||||
args = new String[] {username,password};
|
||||
method = getConnection;
|
||||
} else if ("unwrap".equals(method.getName())) {
|
||||
return unwrap((Class<?>)args[0]);
|
||||
}
|
||||
try {
|
||||
return method.invoke(ds,args);
|
||||
}catch (Throwable t) {
|
||||
if (t instanceof InvocationTargetException
|
||||
&& t.getCause() != null) {
|
||||
throw t.getCause();
|
||||
} else {
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Object unwrap(Class<?> iface) throws SQLException {
|
||||
if (iface == DataSource.class) {
|
||||
return ds;
|
||||
} else {
|
||||
throw new SQLException(sm.getString("dataSourceLinkFactory.badWrapper", iface.getName()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
78
java/org/apache/naming/factory/EjbFactory.java
Normal file
78
java/org/apache/naming/factory/EjbFactory.java
Normal file
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.naming.factory;
|
||||
|
||||
import javax.naming.InitialContext;
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.RefAddr;
|
||||
import javax.naming.Reference;
|
||||
import javax.naming.spi.ObjectFactory;
|
||||
|
||||
import org.apache.naming.EjbRef;
|
||||
|
||||
/**
|
||||
* Object factory for EJBs.
|
||||
*
|
||||
* @author Remy Maucherat
|
||||
*/
|
||||
public class EjbFactory extends FactoryBase {
|
||||
|
||||
@Override
|
||||
protected boolean isReferenceTypeSupported(Object obj) {
|
||||
return obj instanceof EjbRef;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ObjectFactory getDefaultFactory(Reference ref) throws NamingException {
|
||||
|
||||
ObjectFactory factory;
|
||||
String javaxEjbFactoryClassName = System.getProperty(
|
||||
"javax.ejb.Factory", Constants.OPENEJB_EJB_FACTORY);
|
||||
try {
|
||||
factory = (ObjectFactory)
|
||||
Class.forName(javaxEjbFactoryClassName).getConstructor().newInstance();
|
||||
} catch(Throwable t) {
|
||||
if (t instanceof NamingException) {
|
||||
throw (NamingException) t;
|
||||
}
|
||||
if (t instanceof ThreadDeath) {
|
||||
throw (ThreadDeath) t;
|
||||
}
|
||||
if (t instanceof VirtualMachineError) {
|
||||
throw (VirtualMachineError) t;
|
||||
}
|
||||
NamingException ex = new NamingException
|
||||
("Could not create resource factory instance");
|
||||
ex.initCause(t);
|
||||
throw ex;
|
||||
}
|
||||
return factory;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object getLinked(Reference ref) throws NamingException {
|
||||
// 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);
|
||||
return beanObj;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
141
java/org/apache/naming/factory/FactoryBase.java
Normal file
141
java/org/apache/naming/factory/FactoryBase.java
Normal file
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.naming.factory;
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.Name;
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.RefAddr;
|
||||
import javax.naming.Reference;
|
||||
import javax.naming.spi.ObjectFactory;
|
||||
|
||||
import org.apache.naming.StringManager;
|
||||
|
||||
/**
|
||||
* Abstract base class that provides common functionality required by
|
||||
* sub-classes. This class exists primarily to reduce code duplication.
|
||||
*/
|
||||
public abstract class FactoryBase implements ObjectFactory {
|
||||
|
||||
private static final StringManager sm = StringManager.getManager(FactoryBase.class);
|
||||
|
||||
/**
|
||||
* Creates a new object instance.
|
||||
*
|
||||
* @param obj The reference object describing the object to create
|
||||
*/
|
||||
@Override
|
||||
public final Object getObjectInstance(Object obj, Name name, Context nameCtx,
|
||||
Hashtable<?,?> environment) throws Exception {
|
||||
|
||||
if (isReferenceTypeSupported(obj)) {
|
||||
Reference ref = (Reference) obj;
|
||||
|
||||
Object linked = getLinked(ref);
|
||||
if (linked != null) {
|
||||
return linked;
|
||||
}
|
||||
|
||||
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;
|
||||
try {
|
||||
if (tcl != null) {
|
||||
factoryClass = tcl.loadClass(factoryClassName);
|
||||
} else {
|
||||
factoryClass = Class.forName(factoryClassName);
|
||||
}
|
||||
} catch(ClassNotFoundException e) {
|
||||
NamingException ex = new NamingException(sm.getString("factoryBase.factoryClassError"));
|
||||
ex.initCause(e);
|
||||
throw ex;
|
||||
}
|
||||
try {
|
||||
factory = (ObjectFactory) factoryClass.getConstructor().newInstance();
|
||||
} catch(Throwable t) {
|
||||
if (t instanceof NamingException) {
|
||||
throw (NamingException) t;
|
||||
}
|
||||
if (t instanceof ThreadDeath) {
|
||||
throw (ThreadDeath) t;
|
||||
}
|
||||
if (t instanceof VirtualMachineError) {
|
||||
throw (VirtualMachineError) t;
|
||||
}
|
||||
NamingException ex = new NamingException(sm.getString("factoryBase.factoryCreationError"));
|
||||
ex.initCause(t);
|
||||
throw ex;
|
||||
}
|
||||
} else {
|
||||
// Check for a default factory
|
||||
factory = getDefaultFactory(ref);
|
||||
}
|
||||
|
||||
if (factory != null) {
|
||||
return factory.getObjectInstance(obj, name, nameCtx, environment);
|
||||
} else {
|
||||
throw new NamingException(sm.getString("factoryBase.instanceCreationError"));
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determines if this factory supports processing the provided reference
|
||||
* object.
|
||||
*
|
||||
* @param obj The object to be processed
|
||||
*
|
||||
* @return <code>true</code> if this factory can process the object,
|
||||
* otherwise <code>false</code>
|
||||
*/
|
||||
protected abstract boolean isReferenceTypeSupported(Object obj);
|
||||
|
||||
/**
|
||||
* If a default factory is available for the given reference type, create
|
||||
* the default factory.
|
||||
*
|
||||
* @param ref The reference object to be processed
|
||||
*
|
||||
* @return The default factory for the given reference object or
|
||||
* <code>null</code> if no default factory exists.
|
||||
*
|
||||
* @throws NamingException If the default factory cannot be created
|
||||
*/
|
||||
protected abstract ObjectFactory getDefaultFactory(Reference ref)
|
||||
throws NamingException;
|
||||
|
||||
/**
|
||||
* If this reference is a link to another JNDI object, obtain that object.
|
||||
*
|
||||
* @param ref The reference object to be processed
|
||||
*
|
||||
* @return The linked object or <code>null</code> if linked objects are
|
||||
* not supported by or not configured for this reference object
|
||||
* @throws NamingException Error accessing linked object
|
||||
*/
|
||||
protected abstract Object getLinked(Reference ref) throws NamingException;
|
||||
}
|
||||
32
java/org/apache/naming/factory/LocalStrings.properties
Normal file
32
java/org/apache/naming/factory/LocalStrings.properties
Normal file
@@ -0,0 +1,32 @@
|
||||
# Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
# contributor license agreements. See the NOTICE file distributed with
|
||||
# this work for additional information regarding copyright ownership.
|
||||
# The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
# (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
dataSourceLinkFactory.badWrapper=Not a wrapper for type [{0}]
|
||||
|
||||
factoryBase.factoryClassError=Could not load resource factory class
|
||||
factoryBase.factoryCreationError=Could not create resource factory instance
|
||||
factoryBase.instanceCreationError=Could not create resource instance
|
||||
|
||||
lookupFactory.circularReference=Found a circular reference involving [{0}]
|
||||
lookupFactory.createFailed=Could not create instance of JNDI lookup factory class
|
||||
lookupFactory.loadFailed=Could not load JNDI lookup factory class
|
||||
lookupFactory.typeMismatch=The JNDI reference [{0}] was expected to be of type [{1}] but the lookup [{2}] return an object of type [{3}]
|
||||
|
||||
resourceFactory.factoryCreationError=Could not create resource factory instance
|
||||
|
||||
resourceLinkFactory.invalidGlobalContext=Caller provided invalid global context
|
||||
resourceLinkFactory.nullType=The local resource link [{0}] that refers to global resource [{1}] does not specify the required attribute type
|
||||
resourceLinkFactory.unknownType=The local resource link [{0}] that refers to global resource [{1}] specified the unknown type [{2}]
|
||||
resourceLinkFactory.wrongType=The local resource link [{0}] that refers to global resource [{1}] was expected to return an instance of [{2}] but returned an instance of [{3}]
|
||||
20
java/org/apache/naming/factory/LocalStrings_es.properties
Normal file
20
java/org/apache/naming/factory/LocalStrings_es.properties
Normal file
@@ -0,0 +1,20 @@
|
||||
# Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
# contributor license agreements. See the NOTICE file distributed with
|
||||
# this work for additional information regarding copyright ownership.
|
||||
# The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
# (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
lookupFactory.createFailed=No se pudo crear una instancia de la clase de fábrica JNDI lookup\n
|
||||
lookupFactory.typeMismatch=La referencia JNDI [{0}] se esperaba que fuera de tipo [{1}] pero la búsqueda [{2}] devolvió un objeto tipo [{3}]
|
||||
|
||||
resourceLinkFactory.nullType=El enlace del recurso local [{0}] que se refiere al recurso global [{1}] no especifica el atributo obligatorio "type"
|
||||
resourceLinkFactory.unknownType=El enlace del recurso local [{0}] que apunta al recurso global especificado [{1}] de tipo desconocido [{2}]\n
|
||||
32
java/org/apache/naming/factory/LocalStrings_fr.properties
Normal file
32
java/org/apache/naming/factory/LocalStrings_fr.properties
Normal file
@@ -0,0 +1,32 @@
|
||||
# Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
# contributor license agreements. See the NOTICE file distributed with
|
||||
# this work for additional information regarding copyright ownership.
|
||||
# The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
# (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
dataSourceLinkFactory.badWrapper=Pas un enrobeur pour le type [{0}]
|
||||
|
||||
factoryBase.factoryClassError=Impossible de charger la classe de la fabrique de ressources
|
||||
factoryBase.factoryCreationError=Impossible de créer l'instance de la fabrique de ressources
|
||||
factoryBase.instanceCreationError=Impossible de créer l'instance de la ressource
|
||||
|
||||
lookupFactory.circularReference=Trouvé une référence circulaire avec [{0}]
|
||||
lookupFactory.createFailed=Echec de création de l'instance de la classe de fabrique de recherche JNDI
|
||||
lookupFactory.loadFailed=Echec de chargement de la classe de fabrique de recherche JNDI
|
||||
lookupFactory.typeMismatch=La référence JNDI [{0}] devrait être de type [{1}] mais la recherche [{2}] retourne un objet de type [{3}]
|
||||
|
||||
resourceFactory.factoryCreationError=Impossible de créer une instance de la fabrique de ressources
|
||||
|
||||
resourceLinkFactory.invalidGlobalContext=L'appelant a fourni un contexte global invalide
|
||||
resourceLinkFactory.nullType=Le lien local de resource [{0}] qui se réfère à la resource globale [{1}] ne spécifie pas le type d''attribut requis
|
||||
resourceLinkFactory.unknownType=Le lien local de resource [{0}] qui se réfère à la resource globale [{1}] a spécifié le type inconnu [{2}]
|
||||
resourceLinkFactory.wrongType=Le lien de ressource local [{0}] qui se réfère à la ressource globale [{1}] devait renvoyer une instance de [{2}] mais à renvoyé une instance de [{3}]
|
||||
28
java/org/apache/naming/factory/LocalStrings_ja.properties
Normal file
28
java/org/apache/naming/factory/LocalStrings_ja.properties
Normal file
@@ -0,0 +1,28 @@
|
||||
# Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
# contributor license agreements. See the NOTICE file distributed with
|
||||
# this work for additional information regarding copyright ownership.
|
||||
# The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
# (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
dataSourceLinkFactory.badWrapper=クラス[{0}]のラッパーではありません。
|
||||
|
||||
lookupFactory.circularReference=[{0}]を含む循環参照が見つかりました。
|
||||
lookupFactory.createFailed=JNDI lookup ファクトリークラスのインスタンスを作成できませんでした。
|
||||
lookupFactory.loadFailed=JNDIルックアップファクトリクラスをロードできませんでした。
|
||||
lookupFactory.typeMismatch=クラス [{1}] を期待する JNDI 参照 [{0}]に、lookup [{2}] はクラス [{3}] のオブジェクトを返却しました。
|
||||
|
||||
resourceFactory.factoryCreationError=リソースファクトリーのインスタンスを作成できません。
|
||||
|
||||
resourceLinkFactory.invalidGlobalContext=引数に不正な共通コンテキストが指定されました。
|
||||
resourceLinkFactory.nullType=グローバルリソース [{1}] を参照するローカルリソースリンク [{0}] に必要な属性がありません。
|
||||
resourceLinkFactory.unknownType=グローバルリソース [{1}] を参照するローカルリソースリンク [{0}] に未知のクラス [{2}] が指定されました。
|
||||
resourceLinkFactory.wrongType=グローバルリソース[{1}]を参照するローカルリソースリンク[{0}]は[{2}]のインスタンスを返すと予想されましたが、[{3}]のインスタンスを返しました。
|
||||
32
java/org/apache/naming/factory/LocalStrings_ko.properties
Normal file
32
java/org/apache/naming/factory/LocalStrings_ko.properties
Normal file
@@ -0,0 +1,32 @@
|
||||
# Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
# contributor license agreements. See the NOTICE file distributed with
|
||||
# this work for additional information regarding copyright ownership.
|
||||
# The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
# (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
dataSourceLinkFactory.badWrapper=타입 [{0}]을(를) 위한 wrapper가 아닙니다.
|
||||
|
||||
factoryBase.factoryClassError=리소스 팩토리 클래스를 로드하지 못했습니다.
|
||||
factoryBase.factoryCreationError=리소스 팩토리 인스턴스를 생성하지 못했습니다.
|
||||
factoryBase.instanceCreationError=리소스 인스턴스를 생성하지 못했습니다.
|
||||
|
||||
lookupFactory.circularReference=[{0}]을(를) 수반한 순환 참조를 발견했습니다.
|
||||
lookupFactory.createFailed=JNDI lookup 팩토리 클래스의 인스턴스를 생성할 수 없었습니다.
|
||||
lookupFactory.loadFailed=JNDI lookup 팩토리 클래스를 로드할 수 없었습니다.
|
||||
lookupFactory.typeMismatch=JNDI 참조 [{0}]은(는) 타입이 [{1}]이어야 하지만, lookup으로 찾아진 객체 [{2}]은(는) 타입이 [{3}]입니다.
|
||||
|
||||
resourceFactory.factoryCreationError=리소스 팩토리 인스턴스를 생성할 수 없었습니다.
|
||||
|
||||
resourceLinkFactory.invalidGlobalContext=호출자가, 유효하지 않은 글로벌 컨텍스트를 제공했습니다.
|
||||
resourceLinkFactory.nullType=글로벌 리소스 [{1}]을(를) 참조하는 해당 로컬 리소스 링크 [{0}]이(가) 필수적인 속성 타입을 지정하지 않았습니다.
|
||||
resourceLinkFactory.unknownType=글로벌 리소스 [{1}]을(를) 참조하는 해당 로컬 리소스 링크 [{0}]이(가), 알 수 없는 타입 [{2}]으로 지정되어 있습니다.
|
||||
resourceLinkFactory.wrongType=글로벌 리소스 [{1}]을(를) 참조하는 해당 로컬 리소스 링크 [{0}]은(는), 타입 [{2}]의 인스턴스틀 반환할 것으로 기대되었지만, 정작 타입 [{3}]의 인스턴스를 반환했습니다.
|
||||
25
java/org/apache/naming/factory/LocalStrings_zh_CN.properties
Normal file
25
java/org/apache/naming/factory/LocalStrings_zh_CN.properties
Normal file
@@ -0,0 +1,25 @@
|
||||
# Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
# contributor license agreements. See the NOTICE file distributed with
|
||||
# this work for additional information regarding copyright ownership.
|
||||
# The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
# (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
factoryBase.factoryClassError=无法加载资源工厂类
|
||||
factoryBase.factoryCreationError=无法创建资源工厂实例
|
||||
factoryBase.instanceCreationError=无法创建资源实例
|
||||
|
||||
lookupFactory.createFailed=无法创建JNDI查找工厂类实例
|
||||
lookupFactory.loadFailed=无法加载JNDI查找工厂类
|
||||
lookupFactory.typeMismatch=期望JNDI引用[{0}]的类型为[{1}],但查找[{2}]返回类型为[{3}]的对象
|
||||
|
||||
resourceLinkFactory.nullType=引用全局资源 [{1}] 的本地资源链接 [{0}] 未指定所需的属性类型
|
||||
resourceLinkFactory.unknownType=引用全局资源[{1}]的本地资源链接[{0}]指定了未知类型[{2}]
|
||||
155
java/org/apache/naming/factory/LookupFactory.java
Normal file
155
java/org/apache/naming/factory/LookupFactory.java
Normal file
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.naming.factory;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.InitialContext;
|
||||
import javax.naming.Name;
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.RefAddr;
|
||||
import javax.naming.Reference;
|
||||
import javax.naming.spi.ObjectFactory;
|
||||
|
||||
import org.apache.juli.logging.Log;
|
||||
import org.apache.juli.logging.LogFactory;
|
||||
import org.apache.naming.LookupRef;
|
||||
import org.apache.naming.StringManager;
|
||||
|
||||
/**
|
||||
* Object factory for lookups.
|
||||
*/
|
||||
public class LookupFactory implements ObjectFactory {
|
||||
|
||||
private static final Log log = LogFactory.getLog(LookupFactory.class);
|
||||
private static final StringManager sm = StringManager.getManager(LookupFactory.class);
|
||||
|
||||
private static final ThreadLocal<Set<String>> names = new ThreadLocal<Set<String>>() {
|
||||
|
||||
@Override
|
||||
protected Set<String> initialValue() {
|
||||
return new HashSet<>();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a new Resource env instance.
|
||||
*
|
||||
* @param obj The reference object describing the DataSource
|
||||
*/
|
||||
@Override
|
||||
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
|
||||
Hashtable<?, ?> environment) throws Exception {
|
||||
|
||||
String lookupName = null;
|
||||
Object result = null;
|
||||
|
||||
if (obj instanceof LookupRef) {
|
||||
Reference ref = (Reference) obj;
|
||||
ObjectFactory factory = null;
|
||||
RefAddr lookupNameRefAddr = ref.get(LookupRef.LOOKUP_NAME);
|
||||
if (lookupNameRefAddr != null) {
|
||||
lookupName = lookupNameRefAddr.getContent().toString();
|
||||
}
|
||||
|
||||
try {
|
||||
if (lookupName != null) {
|
||||
if (!names.get().add(lookupName)) {
|
||||
String msg = sm.getString("lookupFactory.circularReference", lookupName);
|
||||
NamingException ne = new NamingException(msg);
|
||||
log.warn(msg, ne);
|
||||
throw ne;
|
||||
}
|
||||
}
|
||||
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) {
|
||||
NamingException ex = new NamingException(
|
||||
sm.getString("lookupFactory.loadFailed"));
|
||||
ex.initCause(e);
|
||||
throw ex;
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
factoryClass = Class.forName(factoryClassName);
|
||||
} catch (ClassNotFoundException e) {
|
||||
NamingException ex = new NamingException(
|
||||
sm.getString("lookupFactory.loadFailed"));
|
||||
ex.initCause(e);
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
if (factoryClass != null) {
|
||||
try {
|
||||
factory = (ObjectFactory) factoryClass.getConstructor().newInstance();
|
||||
} catch (Throwable t) {
|
||||
if (t instanceof NamingException)
|
||||
throw (NamingException) t;
|
||||
NamingException ex = new NamingException(
|
||||
sm.getString("lookupFactory.createFailed"));
|
||||
ex.initCause(t);
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Note: No defaults here
|
||||
if (factory != null) {
|
||||
result = factory.getObjectInstance(obj, name, nameCtx, environment);
|
||||
} else {
|
||||
if (lookupName == null) {
|
||||
throw new NamingException(sm.getString("lookupFactory.createFailed"));
|
||||
} else {
|
||||
result = new InitialContext().lookup(lookupName);
|
||||
}
|
||||
}
|
||||
|
||||
Class<?> clazz = Class.forName(ref.getClassName());
|
||||
if (result != null && !clazz.isAssignableFrom(result.getClass())) {
|
||||
String msg = sm.getString("lookupFactory.typeMismatch",
|
||||
name, ref.getClassName(), lookupName, result.getClass().getName());
|
||||
NamingException ne = new NamingException(msg);
|
||||
log.warn(msg, ne);
|
||||
// Close the resource we no longer need if we know how to do so
|
||||
if (result instanceof AutoCloseable) {
|
||||
try {
|
||||
((AutoCloseable) result).close();
|
||||
} catch (Exception e) {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
throw ne;
|
||||
}
|
||||
} finally {
|
||||
names.get().remove(lookupName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
155
java/org/apache/naming/factory/MailSessionFactory.java
Normal file
155
java/org/apache/naming/factory/MailSessionFactory.java
Normal file
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.naming.factory;
|
||||
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.mail.Authenticator;
|
||||
import javax.mail.PasswordAuthentication;
|
||||
import javax.mail.Session;
|
||||
import javax.naming.Context;
|
||||
import javax.naming.Name;
|
||||
import javax.naming.RefAddr;
|
||||
import javax.naming.Reference;
|
||||
import javax.naming.spi.ObjectFactory;
|
||||
|
||||
/**
|
||||
* <p>Factory class that creates a JNDI named JavaMail Session factory,
|
||||
* which can be used for managing inbound and outbound electronic mail
|
||||
* messages via JavaMail APIs. All messaging environment properties
|
||||
* described in the JavaMail Specification may be passed to the Session
|
||||
* factory; however the following properties are the most commonly used:</p>
|
||||
* <ul>
|
||||
* <li>
|
||||
* <li><strong>mail.smtp.host</strong> - Hostname for outbound transport
|
||||
* connections. Defaults to <code>localhost</code> if not specified.</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>This factory can be configured in a
|
||||
* <code><Context></code> element in your <code>conf/server.xml</code>
|
||||
* configuration file. An example of factory configuration is:</p>
|
||||
* <pre>
|
||||
* <Resource name="mail/smtp" auth="CONTAINER"
|
||||
* type="javax.mail.Session"/>
|
||||
* <ResourceParams name="mail/smtp">
|
||||
* <parameter>
|
||||
* <name>factory</name>
|
||||
* <value>org.apache.naming.factory.MailSessionFactory</value>
|
||||
* </parameter>
|
||||
* <parameter>
|
||||
* <name>mail.smtp.host</name>
|
||||
* <value>mail.mycompany.com</value>
|
||||
* </parameter>
|
||||
* </ResourceParams>
|
||||
* </pre>
|
||||
*
|
||||
* @author Craig R. McClanahan
|
||||
*/
|
||||
public class MailSessionFactory implements ObjectFactory {
|
||||
|
||||
|
||||
/**
|
||||
* The Java type for which this factory knows how to create objects.
|
||||
*/
|
||||
protected static final String factoryType = "javax.mail.Session";
|
||||
|
||||
|
||||
/**
|
||||
* Create and return an object instance based on the specified
|
||||
* characteristics.
|
||||
*
|
||||
* @param refObj Reference information containing our parameters, or null
|
||||
* if there are no parameters
|
||||
* @param name The name of this object, relative to context, or null
|
||||
* if there is no name
|
||||
* @param context The context to which name is relative, or null if name
|
||||
* is relative to the default initial context
|
||||
* @param env Environment variables, or null if there are none
|
||||
*
|
||||
* @exception Exception if an error occurs during object creation
|
||||
*/
|
||||
@Override
|
||||
public Object getObjectInstance(Object refObj, Name name, Context context,
|
||||
Hashtable<?,?> env) throws Exception {
|
||||
|
||||
// Return null if we cannot create an object of the requested type
|
||||
final Reference ref = (Reference) refObj;
|
||||
if (!ref.getClassName().equals(factoryType))
|
||||
return null;
|
||||
|
||||
// Create a new Session inside a doPrivileged block, so that JavaMail
|
||||
// can read its default properties without throwing Security
|
||||
// exceptions.
|
||||
//
|
||||
// Bugzilla 31288, 33077: add support for authentication.
|
||||
return AccessController.doPrivileged(new PrivilegedAction<Session>() {
|
||||
@Override
|
||||
public Session run() {
|
||||
|
||||
// Create the JavaMail properties we will use
|
||||
Properties props = new Properties();
|
||||
props.put("mail.transport.protocol", "smtp");
|
||||
props.put("mail.smtp.host", "localhost");
|
||||
|
||||
String password = null;
|
||||
|
||||
Enumeration<RefAddr> attrs = ref.getAll();
|
||||
while (attrs.hasMoreElements()) {
|
||||
RefAddr attr = attrs.nextElement();
|
||||
if ("factory".equals(attr.getType())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ("password".equals(attr.getType())) {
|
||||
password = (String) attr.getContent();
|
||||
continue;
|
||||
}
|
||||
|
||||
props.put(attr.getType(), attr.getContent());
|
||||
}
|
||||
|
||||
Authenticator auth = null;
|
||||
if (password != null) {
|
||||
String user = props.getProperty("mail.smtp.user");
|
||||
if(user == null) {
|
||||
user = props.getProperty("mail.user");
|
||||
}
|
||||
|
||||
if(user != null) {
|
||||
final PasswordAuthentication pa = new PasswordAuthentication(user, password);
|
||||
auth = new Authenticator() {
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return pa;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Create and return the new Session object
|
||||
Session session = Session.getInstance(props, auth);
|
||||
return session;
|
||||
|
||||
}
|
||||
} );
|
||||
}
|
||||
}
|
||||
91
java/org/apache/naming/factory/OpenEjbFactory.java
Normal file
91
java/org/apache/naming/factory/OpenEjbFactory.java
Normal file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
package org.apache.naming.factory;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.InitialContext;
|
||||
import javax.naming.Name;
|
||||
import javax.naming.RefAddr;
|
||||
import javax.naming.Reference;
|
||||
import javax.naming.spi.ObjectFactory;
|
||||
|
||||
import org.apache.naming.EjbRef;
|
||||
|
||||
/**
|
||||
* Object factory for EJBs.
|
||||
*
|
||||
* @author Jacek Laskowski
|
||||
* @author Remy Maucherat
|
||||
*/
|
||||
public class OpenEjbFactory implements ObjectFactory {
|
||||
|
||||
|
||||
// -------------------------------------------------------------- Constants
|
||||
|
||||
|
||||
protected static final String DEFAULT_OPENEJB_FACTORY =
|
||||
"org.openejb.client.LocalInitialContextFactory";
|
||||
|
||||
|
||||
// -------------------------------------------------- ObjectFactory Methods
|
||||
|
||||
|
||||
/**
|
||||
* Create a new EJB instance using OpenEJB.
|
||||
*
|
||||
* @param obj The reference object describing the DataSource
|
||||
*/
|
||||
@Override
|
||||
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
|
||||
Hashtable<?,?> environment)
|
||||
throws Exception {
|
||||
|
||||
Object beanObj = null;
|
||||
|
||||
if (obj instanceof EjbRef) {
|
||||
|
||||
Reference ref = (Reference) obj;
|
||||
|
||||
String factory = DEFAULT_OPENEJB_FACTORY;
|
||||
RefAddr factoryRefAddr = ref.get("openejb.factory");
|
||||
if (factoryRefAddr != null) {
|
||||
// Retrieving the OpenEJB factory
|
||||
factory = factoryRefAddr.getContent().toString();
|
||||
}
|
||||
|
||||
Properties env = new Properties();
|
||||
env.put(Context.INITIAL_CONTEXT_FACTORY, factory);
|
||||
|
||||
RefAddr linkRefAddr = ref.get("openejb.link");
|
||||
if (linkRefAddr != null) {
|
||||
String ejbLink = linkRefAddr.getContent().toString();
|
||||
beanObj = (new InitialContext(env)).lookup(ejbLink);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return beanObj;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
47
java/org/apache/naming/factory/ResourceEnvFactory.java
Normal file
47
java/org/apache/naming/factory/ResourceEnvFactory.java
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.naming.factory;
|
||||
|
||||
import javax.naming.Reference;
|
||||
import javax.naming.spi.ObjectFactory;
|
||||
|
||||
import org.apache.naming.ResourceEnvRef;
|
||||
|
||||
/**
|
||||
* Object factory for Resources env.
|
||||
*
|
||||
* @author Remy Maucherat
|
||||
*/
|
||||
public class ResourceEnvFactory extends FactoryBase {
|
||||
|
||||
@Override
|
||||
protected boolean isReferenceTypeSupported(Object obj) {
|
||||
return obj instanceof ResourceEnvRef;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ObjectFactory getDefaultFactory(Reference ref) {
|
||||
// No default factory supported.
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object getLinked(Reference ref) {
|
||||
// Not supported
|
||||
return null;
|
||||
}
|
||||
}
|
||||
88
java/org/apache/naming/factory/ResourceFactory.java
Normal file
88
java/org/apache/naming/factory/ResourceFactory.java
Normal file
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.naming.factory;
|
||||
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.Reference;
|
||||
import javax.naming.spi.ObjectFactory;
|
||||
|
||||
import org.apache.naming.ResourceRef;
|
||||
import org.apache.naming.StringManager;
|
||||
|
||||
/**
|
||||
* Object factory for Resources.
|
||||
*
|
||||
* @author Remy Maucherat
|
||||
*/
|
||||
public class ResourceFactory extends FactoryBase {
|
||||
|
||||
private static final StringManager sm = StringManager.getManager(ResourceFactory.class);
|
||||
|
||||
@Override
|
||||
protected boolean isReferenceTypeSupported(Object obj) {
|
||||
return obj instanceof ResourceRef;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ObjectFactory getDefaultFactory(Reference ref) throws NamingException {
|
||||
|
||||
ObjectFactory factory = null;
|
||||
|
||||
if (ref.getClassName().equals("javax.sql.DataSource")) {
|
||||
String javaxSqlDataSourceFactoryClassName =
|
||||
System.getProperty("javax.sql.DataSource.Factory",
|
||||
Constants.DBCP_DATASOURCE_FACTORY);
|
||||
try {
|
||||
factory = (ObjectFactory) Class.forName(
|
||||
javaxSqlDataSourceFactoryClassName).getConstructor().newInstance();
|
||||
} catch (Exception e) {
|
||||
NamingException ex = new NamingException(sm.getString("resourceFactory.factoryCreationError"));
|
||||
ex.initCause(e);
|
||||
throw ex;
|
||||
}
|
||||
} else if (ref.getClassName().equals("javax.mail.Session")) {
|
||||
String javaxMailSessionFactoryClassName =
|
||||
System.getProperty("javax.mail.Session.Factory",
|
||||
"org.apache.naming.factory.MailSessionFactory");
|
||||
try {
|
||||
factory = (ObjectFactory) Class.forName(
|
||||
javaxMailSessionFactoryClassName).getConstructor().newInstance();
|
||||
} catch(Throwable t) {
|
||||
if (t instanceof NamingException) {
|
||||
throw (NamingException) t;
|
||||
}
|
||||
if (t instanceof ThreadDeath) {
|
||||
throw (ThreadDeath) t;
|
||||
}
|
||||
if (t instanceof VirtualMachineError) {
|
||||
throw (VirtualMachineError) t;
|
||||
}
|
||||
NamingException ex = new NamingException(sm.getString("resourceFactory.factoryCreationError"));
|
||||
ex.initCause(t);
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
return factory;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object getLinked(Reference ref) {
|
||||
// Not supported
|
||||
return null;
|
||||
}
|
||||
}
|
||||
175
java/org/apache/naming/factory/ResourceLinkFactory.java
Normal file
175
java/org/apache/naming/factory/ResourceLinkFactory.java
Normal file
@@ -0,0 +1,175 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.naming.factory;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.Name;
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.RefAddr;
|
||||
import javax.naming.Reference;
|
||||
import javax.naming.spi.ObjectFactory;
|
||||
|
||||
import org.apache.naming.ResourceLinkRef;
|
||||
import org.apache.naming.StringManager;
|
||||
|
||||
/**
|
||||
* <p>Object factory for resource links.</p>
|
||||
*
|
||||
* @author Remy Maucherat
|
||||
*/
|
||||
public class ResourceLinkFactory implements ObjectFactory {
|
||||
|
||||
// ------------------------------------------------------- Static Variables
|
||||
|
||||
protected static final StringManager sm = StringManager.getManager(ResourceLinkFactory.class);
|
||||
|
||||
/**
|
||||
* Global naming context.
|
||||
*/
|
||||
private static Context globalContext = null;
|
||||
|
||||
private static Map<ClassLoader,Map<String,String>> globalResourceRegistrations =
|
||||
new ConcurrentHashMap<>();
|
||||
|
||||
// --------------------------------------------------------- Public Methods
|
||||
|
||||
/**
|
||||
* Set the global context (note: can only be used once).
|
||||
*
|
||||
* @param newGlobalContext new global context value
|
||||
*/
|
||||
public static void setGlobalContext(Context newGlobalContext) {
|
||||
SecurityManager sm = System.getSecurityManager();
|
||||
if (sm != null) {
|
||||
sm.checkPermission(new RuntimePermission(
|
||||
ResourceLinkFactory.class.getName() + ".setGlobalContext"));
|
||||
}
|
||||
globalContext = newGlobalContext;
|
||||
}
|
||||
|
||||
|
||||
public static void registerGlobalResourceAccess(Context globalContext, String localName,
|
||||
String globalName) {
|
||||
validateGlobalContext(globalContext);
|
||||
ClassLoader cl = Thread.currentThread().getContextClassLoader();
|
||||
Map<String,String> registrations = globalResourceRegistrations.get(cl);
|
||||
if (registrations == null) {
|
||||
// Web application initialization is single threaded so this is
|
||||
// safe.
|
||||
registrations = new HashMap<>();
|
||||
globalResourceRegistrations.put(cl, registrations);
|
||||
}
|
||||
registrations.put(localName, globalName);
|
||||
}
|
||||
|
||||
|
||||
public static void deregisterGlobalResourceAccess(Context globalContext, String localName) {
|
||||
validateGlobalContext(globalContext);
|
||||
ClassLoader cl = Thread.currentThread().getContextClassLoader();
|
||||
Map<String,String> registrations = globalResourceRegistrations.get(cl);
|
||||
if (registrations != null) {
|
||||
registrations.remove(localName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void deregisterGlobalResourceAccess(Context globalContext) {
|
||||
validateGlobalContext(globalContext);
|
||||
ClassLoader cl = Thread.currentThread().getContextClassLoader();
|
||||
globalResourceRegistrations.remove(cl);
|
||||
}
|
||||
|
||||
|
||||
private static void validateGlobalContext(Context globalContext) {
|
||||
if (ResourceLinkFactory.globalContext != null &&
|
||||
ResourceLinkFactory.globalContext != globalContext) {
|
||||
throw new SecurityException(sm.getString("resourceLinkFactory.invalidGlobalContext"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static boolean validateGlobalResourceAccess(String globalName) {
|
||||
ClassLoader cl = Thread.currentThread().getContextClassLoader();
|
||||
while (cl != null) {
|
||||
Map<String,String> registrations = globalResourceRegistrations.get(cl);
|
||||
if (registrations != null && registrations.containsValue(globalName)) {
|
||||
return true;
|
||||
}
|
||||
cl = cl.getParent();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// -------------------------------------------------- ObjectFactory Methods
|
||||
|
||||
/**
|
||||
* Create a new DataSource instance.
|
||||
*
|
||||
* @param obj The reference object describing the DataSource
|
||||
*/
|
||||
@Override
|
||||
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
|
||||
Hashtable<?,?> environment) throws NamingException {
|
||||
|
||||
if (!(obj instanceof ResourceLinkRef)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Can we process this request?
|
||||
Reference ref = (Reference) obj;
|
||||
|
||||
// Read the global ref addr
|
||||
String globalName = null;
|
||||
RefAddr refAddr = ref.get(ResourceLinkRef.GLOBALNAME);
|
||||
if (refAddr != null) {
|
||||
globalName = refAddr.getContent().toString();
|
||||
// Confirm that the current web application is currently configured
|
||||
// to access the specified global resource
|
||||
if (!validateGlobalResourceAccess(globalName)) {
|
||||
return null;
|
||||
}
|
||||
Object result = null;
|
||||
result = globalContext.lookup(globalName);
|
||||
// Check the expected type
|
||||
String expectedClassName = ref.getClassName();
|
||||
if (expectedClassName == null) {
|
||||
throw new IllegalArgumentException(
|
||||
sm.getString("resourceLinkFactory.nullType", name, globalName));
|
||||
}
|
||||
try {
|
||||
Class<?> expectedClazz = Class.forName(
|
||||
expectedClassName, true, Thread.currentThread().getContextClassLoader());
|
||||
if (!expectedClazz.isAssignableFrom(result.getClass())) {
|
||||
throw new IllegalArgumentException(sm.getString("resourceLinkFactory.wrongType",
|
||||
name, globalName, expectedClassName, result.getClass().getName()));
|
||||
}
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new IllegalArgumentException(sm.getString("resourceLinkFactory.unknownType",
|
||||
name, globalName, expectedClassName), e);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
132
java/org/apache/naming/factory/SendMailFactory.java
Normal file
132
java/org/apache/naming/factory/SendMailFactory.java
Normal file
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.naming.factory;
|
||||
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.mail.Session;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import javax.mail.internet.MimePartDataSource;
|
||||
import javax.naming.Context;
|
||||
import javax.naming.Name;
|
||||
import javax.naming.RefAddr;
|
||||
import javax.naming.Reference;
|
||||
import javax.naming.spi.ObjectFactory;
|
||||
|
||||
/**
|
||||
* Factory class that creates a JNDI named javamail MimePartDataSource
|
||||
* object which can be used for sending email using SMTP.
|
||||
* <p>
|
||||
* Can be configured in the Context scope
|
||||
* of your server.xml configuration file.
|
||||
* <p>
|
||||
* Example:
|
||||
* <pre>
|
||||
* <Resource name="mail/send" auth="CONTAINER"
|
||||
* type="javax.mail.internet.MimePartDataSource"/>
|
||||
* <ResourceParams name="mail/send">
|
||||
* <parameter><name>factory</name>
|
||||
* <value>org.apache.naming.factory.SendMailFactory</value>
|
||||
* </parameter>
|
||||
* <parameter><name>mail.smtp.host</name>
|
||||
* <value>your.smtp.host</value>
|
||||
* </parameter>
|
||||
* <parameter><name>mail.smtp.user</name>
|
||||
* <value>someuser</value>
|
||||
* </parameter>
|
||||
* <parameter><name>mail.from</name>
|
||||
* <value>someuser@some.host</value>
|
||||
* </parameter>
|
||||
* <parameter><name>mail.smtp.sendpartial</name>
|
||||
* <value>true</value>
|
||||
* </parameter>
|
||||
* <parameter><name>mail.smtp.dsn.notify</name>
|
||||
* <value>FAILURE</value>
|
||||
* </parameter>
|
||||
* <parameter><name>mail.smtp.dsn.ret</name>
|
||||
* <value>FULL</value>
|
||||
* </parameter>
|
||||
* </ResourceParams>
|
||||
* </pre>
|
||||
*
|
||||
* @author Glenn Nielsen Rich Catlett
|
||||
*/
|
||||
|
||||
public class SendMailFactory implements ObjectFactory
|
||||
{
|
||||
// The class name for the javamail MimeMessageDataSource
|
||||
protected static final String DataSourceClassName =
|
||||
"javax.mail.internet.MimePartDataSource";
|
||||
|
||||
@Override
|
||||
public Object getObjectInstance(Object refObj, Name name, Context ctx,
|
||||
Hashtable<?,?> env) throws Exception {
|
||||
final Reference ref = (Reference)refObj;
|
||||
|
||||
// Creation of the DataSource is wrapped inside a doPrivileged
|
||||
// so that javamail can read its default properties without
|
||||
// throwing Security Exceptions
|
||||
if (ref.getClassName().equals(DataSourceClassName)) {
|
||||
return AccessController.doPrivileged(
|
||||
new PrivilegedAction<MimePartDataSource>()
|
||||
{
|
||||
@Override
|
||||
public MimePartDataSource run() {
|
||||
// set up the smtp session that will send the message
|
||||
Properties props = new Properties();
|
||||
// enumeration of all refaddr
|
||||
Enumeration<RefAddr> list = ref.getAll();
|
||||
// current refaddr to be set
|
||||
RefAddr refaddr;
|
||||
// set transport to smtp
|
||||
props.put("mail.transport.protocol", "smtp");
|
||||
|
||||
while (list.hasMoreElements()) {
|
||||
refaddr = list.nextElement();
|
||||
|
||||
// set property
|
||||
props.put(refaddr.getType(), refaddr.getContent());
|
||||
}
|
||||
MimeMessage message = new MimeMessage(
|
||||
Session.getInstance(props));
|
||||
try {
|
||||
RefAddr fromAddr = ref.get("mail.from");
|
||||
String from = null;
|
||||
if (fromAddr != null) {
|
||||
from = (String)ref.get("mail.from").getContent();
|
||||
}
|
||||
if (from != null) {
|
||||
message.setFrom(new InternetAddress(from));
|
||||
}
|
||||
message.setSubject("");
|
||||
} catch (Exception e) {/*Ignore*/}
|
||||
MimePartDataSource mds = new MimePartDataSource(message);
|
||||
return mds;
|
||||
}
|
||||
} );
|
||||
}
|
||||
else { // We can't create an instance of the DataSource
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
47
java/org/apache/naming/factory/TransactionFactory.java
Normal file
47
java/org/apache/naming/factory/TransactionFactory.java
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.naming.factory;
|
||||
|
||||
import javax.naming.Reference;
|
||||
import javax.naming.spi.ObjectFactory;
|
||||
|
||||
import org.apache.naming.TransactionRef;
|
||||
|
||||
/**
|
||||
* Object factory for User transactions.
|
||||
*
|
||||
* @author Remy Maucherat
|
||||
*/
|
||||
public class TransactionFactory extends FactoryBase {
|
||||
|
||||
@Override
|
||||
protected boolean isReferenceTypeSupported(Object obj) {
|
||||
return obj instanceof TransactionRef;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ObjectFactory getDefaultFactory(Reference ref) {
|
||||
// No default factory supported.
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object getLinked(Reference ref) {
|
||||
// Not supported
|
||||
return null;
|
||||
}
|
||||
}
|
||||
21
java/org/apache/naming/factory/package.html
Normal file
21
java/org/apache/naming/factory/package.html
Normal file
@@ -0,0 +1,21 @@
|
||||
<!--
|
||||
Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
contributor license agreements. See the NOTICE file distributed with
|
||||
this work for additional information regarding copyright ownership.
|
||||
The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
(the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<body>
|
||||
|
||||
<p>This package contains object factories used by the naming service.</p>
|
||||
|
||||
</body>
|
||||
@@ -0,0 +1,16 @@
|
||||
# Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
# contributor license agreements. See the NOTICE file distributed with
|
||||
# this work for additional information regarding copyright ownership.
|
||||
# The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
# (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
serviceProxy.portNotFound=Port-component-ref [{0}] not found
|
||||
@@ -0,0 +1,16 @@
|
||||
# Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
# contributor license agreements. See the NOTICE file distributed with
|
||||
# this work for additional information regarding copyright ownership.
|
||||
# The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
# (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
serviceProxy.portNotFound=Kein Port mit dem Namen [{0}] gefunden
|
||||
@@ -0,0 +1,16 @@
|
||||
# Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
# contributor license agreements. See the NOTICE file distributed with
|
||||
# this work for additional information regarding copyright ownership.
|
||||
# The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
# (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
serviceProxy.portNotFound=Le port-component-ref [{0}] n''a pas été trouvé
|
||||
@@ -0,0 +1,16 @@
|
||||
# Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
# contributor license agreements. See the NOTICE file distributed with
|
||||
# this work for additional information regarding copyright ownership.
|
||||
# The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
# (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
serviceProxy.portNotFound=port-component-ref [{0}] [{0}]が見つかりません
|
||||
@@ -0,0 +1,16 @@
|
||||
# Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
# contributor license agreements. See the NOTICE file distributed with
|
||||
# this work for additional information regarding copyright ownership.
|
||||
# The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
# (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
serviceProxy.portNotFound=Port-component-ref [{0}]을(를) 찾을 수 없습니다.
|
||||
148
java/org/apache/naming/factory/webservices/ServiceProxy.java
Normal file
148
java/org/apache/naming/factory/webservices/ServiceProxy.java
Normal file
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.naming.factory.webservices;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.rmi.Remote;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Iterator;
|
||||
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.rpc.Service;
|
||||
import javax.xml.rpc.ServiceException;
|
||||
|
||||
import org.apache.naming.StringManager;
|
||||
|
||||
/**
|
||||
* Object proxy for Web Services.
|
||||
*
|
||||
* @author Fabien Carrion
|
||||
*/
|
||||
public class ServiceProxy implements InvocationHandler {
|
||||
|
||||
private static final StringManager sm = StringManager.getManager(ServiceProxy.class);
|
||||
|
||||
/**
|
||||
* Service object.
|
||||
* used for delegation
|
||||
*/
|
||||
private final Service service;
|
||||
|
||||
/**
|
||||
* changing behavior to method : Service.getPort(QName, Class)
|
||||
*/
|
||||
private static Method portQNameClass = null;
|
||||
|
||||
/**
|
||||
* changing behavior to method : Service.getPort(Class)
|
||||
*/
|
||||
private static Method portClass = null;
|
||||
|
||||
/**
|
||||
* PortComponentRef list
|
||||
*/
|
||||
private Hashtable<String,QName> portComponentRef = null;
|
||||
|
||||
/**
|
||||
* Constructs a new ServiceProxy wrapping given Service instance.
|
||||
* @param service the wrapped Service instance
|
||||
* @throws ServiceException should be never thrown
|
||||
*/
|
||||
public ServiceProxy(Service service) throws ServiceException {
|
||||
this.service = service;
|
||||
try {
|
||||
portQNameClass = Service.class.getDeclaredMethod("getPort", new Class[]{QName.class, Class.class});
|
||||
portClass = Service.class.getDeclaredMethod("getPort", new Class[]{Class.class});
|
||||
} catch (Exception e) {
|
||||
throw new ServiceException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see InvocationHandler#invoke(Object, Method, Object[])
|
||||
*/
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
|
||||
if (portQNameClass.equals(method)) {
|
||||
return getProxyPortQNameClass(args);
|
||||
}
|
||||
|
||||
if (portClass.equals(method)) {
|
||||
return getProxyPortClass(args);
|
||||
}
|
||||
|
||||
try {
|
||||
return method.invoke(service, args);
|
||||
} catch (InvocationTargetException ite) {
|
||||
throw ite.getTargetException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param args Method call arguments
|
||||
* @return Returns the correct Port
|
||||
* @throws ServiceException if port's QName is an unknown Port (not defined in WSDL).
|
||||
*/
|
||||
private Object getProxyPortQNameClass(Object[] args) throws ServiceException {
|
||||
QName name = (QName) args[0];
|
||||
String nameString = name.getLocalPart();
|
||||
Class<?> serviceendpointClass = (Class<?>) args[1];
|
||||
|
||||
for (@SuppressWarnings("unchecked") Iterator<QName> ports = service.getPorts(); ports.hasNext();) {
|
||||
QName portName = ports.next();
|
||||
String portnameString = portName.getLocalPart();
|
||||
if (portnameString.equals(nameString)) {
|
||||
return service.getPort(name, serviceendpointClass);
|
||||
}
|
||||
}
|
||||
|
||||
// no ports have been found
|
||||
throw new ServiceException(sm.getString("serviceProxy.portNotFound", name));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param portComponentRef List
|
||||
*/
|
||||
public void setPortComponentRef(Hashtable<String,QName> portComponentRef) {
|
||||
this.portComponentRef = portComponentRef;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param args Method call arguments
|
||||
* @return Returns the correct Port
|
||||
* @throws ServiceException if port's QName is an unknown Port
|
||||
*/
|
||||
private Remote getProxyPortClass(Object[] args) throws ServiceException {
|
||||
Class<?> serviceendpointClass = (Class<?>) args[0];
|
||||
|
||||
if (this.portComponentRef == null) {
|
||||
return service.getPort(serviceendpointClass);
|
||||
}
|
||||
|
||||
QName portname = this.portComponentRef.get(serviceendpointClass.getName());
|
||||
if (portname != null) {
|
||||
return service.getPort(portname, serviceendpointClass);
|
||||
} else {
|
||||
return service.getPort(serviceendpointClass);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,359 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.naming.factory.webservices;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.Name;
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.RefAddr;
|
||||
import javax.naming.spi.ObjectFactory;
|
||||
import javax.wsdl.Definition;
|
||||
import javax.wsdl.Port;
|
||||
import javax.wsdl.extensions.ExtensibilityElement;
|
||||
import javax.wsdl.extensions.soap.SOAPAddress;
|
||||
import javax.wsdl.factory.WSDLFactory;
|
||||
import javax.wsdl.xml.WSDLReader;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.rpc.Service;
|
||||
import javax.xml.rpc.ServiceFactory;
|
||||
import javax.xml.rpc.handler.Handler;
|
||||
import javax.xml.rpc.handler.HandlerChain;
|
||||
import javax.xml.rpc.handler.HandlerInfo;
|
||||
import javax.xml.rpc.handler.HandlerRegistry;
|
||||
|
||||
import org.apache.naming.HandlerRef;
|
||||
import org.apache.naming.ServiceRef;
|
||||
|
||||
/**
|
||||
* Object factory for Web Services.
|
||||
*
|
||||
* @author Fabien Carrion
|
||||
*/
|
||||
public class ServiceRefFactory implements ObjectFactory {
|
||||
|
||||
/**
|
||||
* Create a new serviceref instance.
|
||||
*
|
||||
* @param obj The reference object describing the webservice
|
||||
*/
|
||||
@Override
|
||||
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
|
||||
Hashtable<?,?> environment) throws Exception {
|
||||
|
||||
if (obj instanceof ServiceRef) {
|
||||
ServiceRef ref = (ServiceRef) obj;
|
||||
|
||||
// ClassLoader
|
||||
ClassLoader tcl = Thread.currentThread().getContextClassLoader();
|
||||
if (tcl == null) {
|
||||
tcl = this.getClass().getClassLoader();
|
||||
}
|
||||
ServiceFactory factory = ServiceFactory.newInstance();
|
||||
javax.xml.rpc.Service service = null;
|
||||
|
||||
// Service Interface
|
||||
RefAddr tmp = ref.get(ServiceRef.SERVICE_INTERFACE);
|
||||
String serviceInterface = null;
|
||||
if (tmp != null) {
|
||||
serviceInterface = (String) tmp.getContent();
|
||||
}
|
||||
|
||||
// WSDL
|
||||
tmp = ref.get(ServiceRef.WSDL);
|
||||
String wsdlRefAddr = null;
|
||||
if (tmp != null) {
|
||||
wsdlRefAddr = (String) tmp.getContent();
|
||||
}
|
||||
|
||||
// PortComponent
|
||||
Hashtable<String,QName> portComponentRef = new Hashtable<>();
|
||||
|
||||
// Create QName object
|
||||
QName serviceQname = null;
|
||||
tmp = ref.get(ServiceRef.SERVICE_LOCAL_PART);
|
||||
if (tmp != null) {
|
||||
String serviceLocalPart = (String) tmp.getContent();
|
||||
tmp = ref.get(ServiceRef.SERVICE_NAMESPACE);
|
||||
if (tmp == null) {
|
||||
serviceQname = new QName(serviceLocalPart);
|
||||
} else {
|
||||
String serviceNamespace = (String) tmp.getContent();
|
||||
serviceQname = new QName(serviceNamespace,
|
||||
serviceLocalPart);
|
||||
}
|
||||
}
|
||||
Class<?> serviceInterfaceClass = null;
|
||||
|
||||
// Create service object
|
||||
if (serviceInterface == null) {
|
||||
if (serviceQname == null) {
|
||||
throw new NamingException
|
||||
("Could not create service-ref instance");
|
||||
}
|
||||
try {
|
||||
if (wsdlRefAddr == null) {
|
||||
service = factory.createService( serviceQname );
|
||||
} else {
|
||||
service = factory.createService( new URL(wsdlRefAddr),
|
||||
serviceQname );
|
||||
}
|
||||
} catch (Exception e) {
|
||||
NamingException ex = new NamingException("Could not create service");
|
||||
ex.initCause(e);
|
||||
throw ex;
|
||||
}
|
||||
} else {
|
||||
// Loading service Interface
|
||||
try {
|
||||
serviceInterfaceClass = tcl.loadClass(serviceInterface);
|
||||
} catch(ClassNotFoundException e) {
|
||||
NamingException ex = new NamingException("Could not load service Interface");
|
||||
ex.initCause(e);
|
||||
throw ex;
|
||||
}
|
||||
if (serviceInterfaceClass == null) {
|
||||
throw new NamingException
|
||||
("Could not load service Interface");
|
||||
}
|
||||
try {
|
||||
if (wsdlRefAddr == null) {
|
||||
if (!Service.class.isAssignableFrom(serviceInterfaceClass)) {
|
||||
throw new NamingException("service Interface should extend javax.xml.rpc.Service");
|
||||
}
|
||||
service = factory.loadService( serviceInterfaceClass );
|
||||
} else {
|
||||
service = factory.loadService( new URL(wsdlRefAddr),
|
||||
serviceInterfaceClass,
|
||||
new Properties() );
|
||||
}
|
||||
} catch (Exception e) {
|
||||
NamingException ex = new NamingException("Could not create service");
|
||||
ex.initCause(e);
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
if (service == null) {
|
||||
throw new NamingException
|
||||
("Cannot create service object");
|
||||
}
|
||||
serviceQname = service.getServiceName();
|
||||
serviceInterfaceClass = service.getClass();
|
||||
if (wsdlRefAddr != null) {
|
||||
try {
|
||||
WSDLFactory wsdlfactory = WSDLFactory.newInstance();
|
||||
WSDLReader reader = wsdlfactory.newWSDLReader();
|
||||
reader.setFeature("javax.wsdl.importDocuments", true);
|
||||
Definition def = reader.readWSDL((new URL(wsdlRefAddr)).toExternalForm());
|
||||
|
||||
javax.wsdl.Service wsdlservice = def.getService(serviceQname);
|
||||
@SuppressWarnings("unchecked") // Can't change the API
|
||||
Map<String,?> ports = wsdlservice.getPorts();
|
||||
Method m = serviceInterfaceClass.getMethod("setEndpointAddress",
|
||||
new Class[] { java.lang.String.class, java.lang.String.class });
|
||||
for (String portName : ports.keySet()) {
|
||||
Port port = wsdlservice.getPort(portName);
|
||||
String endpoint = getSOAPLocation(port);
|
||||
m.invoke(service, new Object[]{port.getName(), endpoint});
|
||||
portComponentRef.put(endpoint, new QName(port.getName()));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (e instanceof InvocationTargetException) {
|
||||
Throwable cause = e.getCause();
|
||||
if (cause instanceof ThreadDeath) {
|
||||
throw (ThreadDeath) cause;
|
||||
}
|
||||
if (cause instanceof VirtualMachineError) {
|
||||
throw (VirtualMachineError) cause;
|
||||
}
|
||||
}
|
||||
NamingException ex = new NamingException("Error while reading Wsdl File");
|
||||
ex.initCause(e);
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
ServiceProxy proxy = new ServiceProxy(service);
|
||||
|
||||
// Use port-component-ref
|
||||
for (int i = 0; i < ref.size(); i++) {
|
||||
if (ServiceRef.SERVICEENDPOINTINTERFACE.equals(ref.get(i).getType())) {
|
||||
String serviceendpoint = "";
|
||||
String portlink = "";
|
||||
serviceendpoint = (String) ref.get(i).getContent();
|
||||
if (ServiceRef.PORTCOMPONENTLINK.equals(ref.get(i + 1).getType())) {
|
||||
i++;
|
||||
portlink = (String) ref.get(i).getContent();
|
||||
}
|
||||
portComponentRef.put(serviceendpoint, new QName(portlink));
|
||||
|
||||
}
|
||||
}
|
||||
proxy.setPortComponentRef(portComponentRef);
|
||||
|
||||
// Instantiate service with proxy class
|
||||
Class<?>[] serviceInterfaces = serviceInterfaceClass.getInterfaces();
|
||||
|
||||
Class<?>[] interfaces = Arrays.copyOf(serviceInterfaces, serviceInterfaces.length + 1);
|
||||
interfaces[interfaces.length - 1] = javax.xml.rpc.Service.class;
|
||||
|
||||
Object proxyInstance = null;
|
||||
try {
|
||||
proxyInstance = Proxy.newProxyInstance(tcl, interfaces, proxy);
|
||||
} catch (IllegalArgumentException e) {
|
||||
proxyInstance = Proxy.newProxyInstance(tcl, serviceInterfaces, proxy);
|
||||
}
|
||||
|
||||
// Use handler
|
||||
if (ref.getHandlersSize() > 0) {
|
||||
|
||||
HandlerRegistry handlerRegistry = service.getHandlerRegistry();
|
||||
List<String> soaproles = new ArrayList<>();
|
||||
|
||||
while (ref.getHandlersSize() > 0) {
|
||||
HandlerRef handlerRef = ref.getHandler();
|
||||
HandlerInfo handlerInfo = new HandlerInfo();
|
||||
|
||||
// Loading handler Class
|
||||
tmp = handlerRef.get(HandlerRef.HANDLER_CLASS);
|
||||
if ((tmp == null) || (tmp.getContent() == null)) {
|
||||
break;
|
||||
}
|
||||
Class<?> handlerClass = null;
|
||||
try {
|
||||
handlerClass = tcl.loadClass((String) tmp.getContent());
|
||||
} catch(ClassNotFoundException e) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Load all datas relative to the handler : SOAPHeaders, config init element,
|
||||
// portNames to be set on
|
||||
List<QName> headers = new ArrayList<>();
|
||||
Hashtable<String,String> config = new Hashtable<>();
|
||||
List<String> portNames = new ArrayList<>();
|
||||
for (int i = 0; i < handlerRef.size(); i++) {
|
||||
if (HandlerRef.HANDLER_LOCALPART.equals(handlerRef.get(i).getType())) {
|
||||
String localpart = "";
|
||||
String namespace = "";
|
||||
localpart = (String) handlerRef.get(i).getContent();
|
||||
if (HandlerRef.HANDLER_NAMESPACE.equals(handlerRef.get(i + 1).getType())) {
|
||||
i++;
|
||||
namespace = (String) handlerRef.get(i).getContent();
|
||||
}
|
||||
QName header = new QName(namespace, localpart);
|
||||
headers.add(header);
|
||||
} else if (HandlerRef.HANDLER_PARAMNAME.equals(handlerRef.get(i).getType())) {
|
||||
String paramName = "";
|
||||
String paramValue = "";
|
||||
paramName = (String) handlerRef.get(i).getContent();
|
||||
if (HandlerRef.HANDLER_PARAMVALUE.equals(handlerRef.get(i + 1).getType())) {
|
||||
i++;
|
||||
paramValue = (String) handlerRef.get(i).getContent();
|
||||
}
|
||||
config.put(paramName, paramValue);
|
||||
} else if (HandlerRef.HANDLER_SOAPROLE.equals(handlerRef.get(i).getType())) {
|
||||
String soaprole = "";
|
||||
soaprole = (String) handlerRef.get(i).getContent();
|
||||
soaproles.add(soaprole);
|
||||
} else if (HandlerRef.HANDLER_PORTNAME.equals(handlerRef.get(i).getType())) {
|
||||
String portName = "";
|
||||
portName = (String) handlerRef.get(i).getContent();
|
||||
portNames.add(portName);
|
||||
}
|
||||
}
|
||||
|
||||
// Set the handlers informations
|
||||
handlerInfo.setHandlerClass(handlerClass);
|
||||
handlerInfo.setHeaders(headers.toArray(new QName[headers.size()]));
|
||||
handlerInfo.setHandlerConfig(config);
|
||||
|
||||
if (!portNames.isEmpty()) {
|
||||
for (String portName : portNames) {
|
||||
initHandlerChain(new QName(portName), handlerRegistry,
|
||||
handlerInfo, soaproles);
|
||||
}
|
||||
} else {
|
||||
Enumeration<QName> e = portComponentRef.elements();
|
||||
while(e.hasMoreElements()) {
|
||||
initHandlerChain(e.nextElement(), handlerRegistry, handlerInfo, soaproles);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return proxyInstance;
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param port analyzed port
|
||||
* @return Returns the endpoint URL of the given Port
|
||||
*/
|
||||
private String getSOAPLocation(Port port) {
|
||||
String endpoint = null;
|
||||
@SuppressWarnings("unchecked") // Can't change the API
|
||||
List<ExtensibilityElement> extensions = port.getExtensibilityElements();
|
||||
for (ExtensibilityElement ext : extensions) {
|
||||
if (ext instanceof SOAPAddress) {
|
||||
SOAPAddress addr = (SOAPAddress) ext;
|
||||
endpoint = addr.getLocationURI();
|
||||
}
|
||||
}
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
|
||||
private void initHandlerChain(QName portName, HandlerRegistry handlerRegistry,
|
||||
HandlerInfo handlerInfo, List<String> soaprolesToAdd) {
|
||||
HandlerChain handlerChain = (HandlerChain) handlerRegistry.getHandlerChain(portName);
|
||||
@SuppressWarnings("unchecked") // Can't change the API
|
||||
Iterator<Handler> iter = handlerChain.iterator();
|
||||
while (iter.hasNext()) {
|
||||
Handler handler = iter.next();
|
||||
handler.init(handlerInfo);
|
||||
}
|
||||
String[] soaprolesRegistered = handlerChain.getRoles();
|
||||
String [] soaproles = new String[soaprolesRegistered.length + soaprolesToAdd.size()];
|
||||
int i;
|
||||
for (i = 0;i < soaprolesRegistered.length; i++) {
|
||||
soaproles[i] = soaprolesRegistered[i];
|
||||
}
|
||||
for (int j = 0; j < soaprolesToAdd.size(); j++) {
|
||||
soaproles[i+j] = soaprolesToAdd.get(j);
|
||||
}
|
||||
handlerChain.setRoles(soaproles);
|
||||
handlerRegistry.setHandlerChain(portName, handlerChain);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user