001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.tomcat.common;
018:
019: import org.apache.naming.EjbRef;
020:
021: import javax.naming.RefAddr;
022: import javax.naming.Reference;
023: import java.util.HashMap;
024: import java.util.Map;
025: import java.util.concurrent.atomic.AtomicInteger;
026:
027: public class NamingUtil {
028: public static final String NAME = "name";
029: public static final String DEPLOYMENT_ID = "deploymentid";
030: public static final String EXTERNAL = "external";
031: public static final String LOCAL = "local";
032: public static final String REMOTE = EjbRef.REMOTE;
033: public static final String JNDI_NAME = "jndiname";
034: public static final String JNDI_PROVIDER_ID = "jndiproviderid";
035: public static final String UNIT = "unit";
036: public static final String EXTENDED = "extended";
037: public static final String PROPERTIES = "properties";
038: public static final String RESOURCE_ID = "resourceid";
039: public static final String COMPONENT_TYPE = "componenttype";
040: public static final String WS_ID = "wsid";
041: public static final String WS_CLASS = "wsclass";
042: public static final String WS_QNAME = "wsqname";
043: public static final String WS_PORT_QNAME = "wsportqname";
044: public static final String WSDL_URL = "wsdlurl";
045:
046: private static final AtomicInteger id = new AtomicInteger(31);
047: private static final Map<String, Object> registry = new HashMap<String, Object>();
048:
049: public static String getProperty(Reference ref, String name) {
050: RefAddr addr = ref.get(name);
051: if (addr == null)
052: return null;
053: Object value = addr.getContent();
054: return (String) value;
055: }
056:
057: public static boolean isPropertyTrue(Reference ref, String name) {
058: RefAddr addr = ref.get(name);
059: if (addr == null)
060: return false;
061: Object value = addr.getContent();
062: return Boolean.parseBoolean("" + value);
063: }
064:
065: public static void setStaticValue(Resource resource, Object value) {
066: setStaticValue(resource, null, value);
067: }
068:
069: public static void setStaticValue(Resource resource, String name,
070: Object value) {
071: name = name != null ? "-" + name : "";
072: String token = "" + id.incrementAndGet();
073: registry.put(token, value);
074: resource.setProperty("static-token" + name, token);
075: }
076:
077: @SuppressWarnings({"unchecked"})
078: public static <T> T getStaticValue(Reference ref) {
079: return (T) getStaticValue(ref, null);
080: }
081:
082: @SuppressWarnings({"unchecked"})
083: public static <T> T getStaticValue(Reference ref, String name) {
084: name = name != null ? "-" + name : "";
085: String token = getProperty(ref, "static-token" + name);
086: if (token == null) {
087: return null;
088: }
089: T object = (T) registry.get(token);
090: return object;
091: }
092:
093: public static Class<?> loadClass(String className) {
094: if (className == null)
095: return null;
096: try {
097: ClassLoader classLoader = Thread.currentThread()
098: .getContextClassLoader();
099: if (classLoader != null) {
100: try {
101: Class clazz = classLoader.loadClass(className);
102: return clazz;
103: } catch (ClassNotFoundException e) {
104: }
105: }
106: return Class.forName(className);
107: } catch (ClassNotFoundException e) {
108: return null;
109: }
110: }
111:
112: /**
113: * This interface exists because the class org.apache.catalina.deploy.ContextResource
114: * is not available in the common classloader in tomcat 55
115: */
116: public interface Resource {
117: void setProperty(String name, Object value);
118: }
119:
120: }
|