001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.identity.profile.api.configurator.impl.dynamic;
042:
043: import java.io.File;
044: import java.io.FileInputStream;
045: import java.lang.reflect.Method;
046: import java.net.MalformedURLException;
047: import java.net.URL;
048: import java.net.URLClassLoader;
049: import java.util.HashMap;
050: import java.util.Map;
051: import java.util.Properties;
052: import org.netbeans.modules.identity.profile.api.configurator.ConfiguratorException;
053: import org.netbeans.modules.identity.profile.api.configurator.ServerProperties;
054: import org.openide.util.NbBundle;
055:
056: /**
057: * This class manages classloaders for each instance of the AM client
058: * sdk. This is used to support local and remote AM server instances.
059: *
060: * @author ptliu
061: */
062: class ClassLoaderManager {
063:
064: private static final String AM_SYSTEM_PROPERTIES_CLASS = "com.iplanet.am.util.SystemProperties"; //NOI18N
065:
066: private static final String AM_INITIALIZE_PROPERTIES_METHOD = "initializeProperties"; //NOI18N
067:
068: private static String[] dependentJars = {
069: "/addons/accessmanager/amclientsdk.jar", //NOI18N
070: "/lib/javaee.jar", //NOI18N
071: "/lib/webservices-rt.jar" //NOI18N
072: };
073:
074: private static ClassLoaderManager instance;
075:
076: private Map<String, AMClientSDKClassLoader> classLoaderCache = new HashMap<String, AMClientSDKClassLoader>();
077:
078: private ClassLoaderManager() {
079:
080: }
081:
082: public static ClassLoaderManager getDefault() {
083: if (instance == null) {
084: instance = new ClassLoaderManager();
085: }
086:
087: return instance;
088: }
089:
090: /**
091: * TODO: Need to pass in configuration information for the amclientsdk.
092: *
093: */
094: public ClassLoader getClassLoader(ServerProperties properties) {
095: String key = properties.getProperty(ServerProperties.PROP_ID);
096: AMClientSDKClassLoader classLoader = classLoaderCache.get(key);
097:
098: // First check to see if the server properties have changed.
099: // If so, we need to recreate the class loader.
100: if (classLoader != null) {
101: if (!properties.equals(classLoader.getProperties())) {
102: classLoader = null;
103: }
104: }
105:
106: if (classLoader == null) {
107: classLoader = createClassLoader(properties);
108: classLoaderCache.put(key, classLoader);
109: }
110:
111: return classLoader;
112: }
113:
114: public void removeClassLoader(ServerProperties properties) {
115: classLoaderCache.remove(properties
116: .getProperty(ServerProperties.PROP_ID));
117: }
118:
119: private AMClientSDKClassLoader createClassLoader(
120: ServerProperties properties) {
121: String asRoot = properties
122: .getProperty(ServerProperties.PROP_AS_ROOT);
123:
124: //System.out.println("asRoot = " + asRoot);
125:
126: if (asRoot != null) {
127: URL[] urls = new URL[dependentJars.length];
128:
129: for (int i = 0; i < dependentJars.length; i++) {
130: File jarFile = new File(asRoot + dependentJars[i]);
131:
132: try {
133: URL url = jarFile.toURL();
134: urls[i] = url;
135: } catch (MalformedURLException ex) {
136: // ignore
137: }
138: }
139:
140: return new AMClientSDKClassLoader(urls, properties);
141: } else {
142: return new AMClientSDKClassLoader(new URL[] {},
143: ClassLoaderManager.class.getClassLoader(),
144: properties);
145: }
146: }
147:
148: private static class AMClientSDKClassLoader extends URLClassLoader {
149: private ServerProperties properties;
150:
151: public AMClientSDKClassLoader(URL[] urls,
152: ServerProperties properties) {
153: super (urls);
154:
155: init(properties);
156: }
157:
158: public AMClientSDKClassLoader(URL[] urls, ClassLoader parent,
159: ServerProperties properties) {
160: super (urls, parent);
161:
162: init(properties);
163: }
164:
165: private void init(ServerProperties properties) {
166: this .properties = (ServerProperties) properties.clone();
167:
168: try {
169: Class systemPropClazz = loadClass(AM_SYSTEM_PROPERTIES_CLASS);
170: Method method = systemPropClazz.getMethod(
171: AM_INITIALIZE_PROPERTIES_METHOD,
172: Properties.class);
173: method.invoke(null, properties);
174: } catch (ClassNotFoundException ex) {
175: throw new ConfiguratorException(NbBundle.getMessage(
176: ClassLoaderManager.class,
177: "TXT_FailedToLoadAMClientSDK"));
178: } catch (Exception ex) {
179: throw ConfiguratorException.create(ex);
180: }
181: }
182:
183: public ServerProperties getProperties() {
184: return properties;
185: }
186: }
187: }
|