
org.ow2.bonita.util.AccessorUtil Maven / Gradle / Ivy
/**
* Copyright (C) 2006 Bull S. A. S.
* Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation
* version 2.1 of the License.
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
* Floor, Boston, MA 02110-1301, USA.
**/
package org.ow2.bonita.util;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import org.ow2.bonita.facade.APIAccessor;
import org.ow2.bonita.facade.CommandAPI;
import org.ow2.bonita.facade.Context;
import org.ow2.bonita.facade.ManagementAPI;
import org.ow2.bonita.facade.QueryAPIAccessor;
import org.ow2.bonita.facade.QueryDefinitionAPI;
import org.ow2.bonita.facade.QueryRuntimeAPI;
import org.ow2.bonita.facade.RuntimeAPI;
import org.ow2.bonita.facade.ejb.ejb2.EJB2APIAccessorImpl;
import org.ow2.bonita.facade.ejb.ejb2.EJB2QueryAPIAccessorImpl;
import org.ow2.bonita.facade.ejb.ejb3.EJB3APIAccessorImpl;
import org.ow2.bonita.facade.ejb.ejb3.EJB3QueryAPIAccessorImpl;
import org.ow2.bonita.facade.impl.StandardAPIAccessorImpl;
import org.ow2.bonita.facade.impl.StandardInternalAPIAccessorFactory;
import org.ow2.bonita.facade.impl.StandardQueryAPIAccessorImpl;
import org.ow2.bonita.facade.internal.InternalRuntimeAPI;
/**
*This utility class has been provided to unify access to Bonita APIs and to
* avoid the use of lookups in JEE deployments:
* org.ow2.bonita.util.AccessorUtil.
*Through this class, Nova Bonita APIs can be reached in a unified way in both
* local and remote applications.
*For that to be done, the system property called "org.ow2.bonita.api-type"
* must be defined at client side to specify whether the APIs will be reached
* locally or remotely
*(possible values are "standard", "auto-detect", "ejb2" and "ejb3").
*/
public final class AccessorUtil {
public static final String API_TYPE_PROPERTY = BonitaConstants.API_TYPE_PROPERTY;
public static final String RUNTIMEAPI_JNDINAME = "runtimeAPI";
public static final String MANAGEMENT_JNDINAME = "managementAPI";
public static final String DEFINITIONAPI_JNDINAME = "definitionAPI";
public static final String QUERYRUNTIME_JNDINAME = "queryRuntimeAPI";
public static final String QUERYDEFINITION_JNDINAME = "queryDefinitionAPI";
public static final String COMMANDAPI_JNDINAME = "commandAPI";
private static final Logger LOG = Logger.getLogger(AccessorUtil.class.getName());
protected static final ThreadLocal CONTEXT = new ThreadLocal() {
// Static initialization
@Override
protected Context initialValue() {
return resetContext();
}
};
public static Context resetContext() {
String apiType = System.getProperty(API_TYPE_PROPERTY);
if (System.getProperty(API_TYPE_PROPERTY) != null) {
Context context = Misc.stringToEnum(Context.class, apiType);
if (LOG.isLoggable(Level.INFO)) {
LOG.info("API-Type: " + context + " has been specified through property: " + API_TYPE_PROPERTY);
}
return context;
}
if (LOG.isLoggable(Level.INFO)) {
LOG.info("Property: " + API_TYPE_PROPERTY + " has not been specified for api-type. Trying to autodetect it.");
}
//check if the call was done from server side or client side
try {
StandardInternalAPIAccessorFactory.getStandardInternalAPIAccessor();
if (LOG.isLoggable(Level.INFO)) {
LOG.info(AccessorUtil.class.getName() + " called from server side. Using " + Context.Standard + " context.");
}
return Context.Standard;
} catch (BonitaRuntimeException e) {
//not called from server
if (LOG.isLoggable(Level.INFO)) {
LOG.info(AccessorUtil.class.getName() + " called from client side. Trying to autodetect apiType.");
}
try {
InternalRuntimeAPI.class.cast(Misc.lookup(RUNTIMEAPI_JNDINAME, null));
if (LOG.isLoggable(Level.INFO)) {
LOG.info(Context.EJB3 + " context found: EJB3 api-type will be used.");
}
return Context.EJB3;
} catch (final NamingException ne) {
if (LOG.isLoggable(Level.INFO)) {
LOG.info("No context found: assuming J2SE. Standard api-type will be used."
+ " Note that this may not be what you want."
+ " In this case, either specify the api-type in the environment or via the property: "
+ API_TYPE_PROPERTY);
}
return Context.Standard;
} catch (final ClassCastException cceEJB3) {
return Context.EJB2;
}
}
}
private AccessorUtil() {
}
public static QueryAPIAccessor getQueryAPIAccessor(Hashtable jndiEnvironment) {
switch (AccessorUtil.CONTEXT.get()) {
case EJB2:
return new EJB2QueryAPIAccessorImpl(jndiEnvironment);
case EJB3:
return new EJB3QueryAPIAccessorImpl(jndiEnvironment);
default:
return new StandardQueryAPIAccessorImpl();
}
}
public static APIAccessor getAPIAccessor(Hashtable jndiEnvironment) {
switch (AccessorUtil.CONTEXT.get()) {
case EJB2:
return new EJB2APIAccessorImpl(jndiEnvironment);
case EJB3:
return new EJB3APIAccessorImpl(jndiEnvironment);
default:
return new StandardAPIAccessorImpl();
}
}
/**
* To get the APIAccessor interface.
*
* @return the interface APIAccessor.
*/
public static APIAccessor getAPIAccessor() {
return getAPIAccessor(null);
}
/**
* To get the QueryAPIAccessor interface.
*
* @return the interface QueryAPIAccessor.
*/
public static QueryAPIAccessor getQueryAPIAccessor() {
return getQueryAPIAccessor(null);
}
/**
* To get the RuntimeAPI interface.
*
* @return the interface RuntimeAPI.
*/
public static RuntimeAPI getRuntimeAPI() {
return getAPIAccessor().getRuntimeAPI();
}
/**
* To get the ManagementAPI interface.
*
* @return the interface ManagementAPI.
*/
public static ManagementAPI getManagementAPI() {
return getAPIAccessor().getManagementAPI();
}
/**
* To get the CommandAPI interface.
*
* @return the interface CommandAPI.
*/
public static CommandAPI getCommandAPI() {
return getAPIAccessor().getCommandAPI();
}
/**
* To get the QueryRuntimeAPI interface.
*
* @return the interface QueryRuntimeAPI.
*/
public static QueryRuntimeAPI getQueryRuntimeAPI() {
return getAPIAccessor().getQueryRuntimeAPI();
}
/**
* To get the QueryRuntimeAPI interface.
*
* @param the
* name of the list of queriers to use (this name should be defined
* in the environment).
* @return the interface QueryRuntimeAPI.
*/
// public static QueryRuntimeAPI getQueryRuntimeAPI(final String queryList) {
// return getAPIAccessor().getQueryRuntimeAPI(queryList);
// }
/**
* To get the QueryDefinitionAPI interface.
*
* @return the interface QueryDefinitionAPI.
*/
public static QueryDefinitionAPI getQueryDefinitionAPI() {
return getAPIAccessor().getQueryDefinitionAPI();
}
/**
* To get the QueryDefinitionAPI interface.
*
* @param the
* name of the list of queriers to use (this name should be defined
* in the environment).
* @return the interface QueryDefinitionAPI.
*/
// public static QueryDefinitionAPI getQueryDefinitionAPI(final String
// queryList) {
// return getAPIAccessor().getQueryDefinitionAPI(queryList);
// }
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy