001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.jbi.framework;
018
019 import java.beans.PropertyChangeListener;
020 import java.io.File;
021 import java.net.MalformedURLException;
022 import java.net.URL;
023
024 import javax.management.JMException;
025 import javax.management.MBeanAttributeInfo;
026 import javax.management.MBeanOperationInfo;
027
028 import org.apache.servicemix.jbi.deployment.ClassPath;
029 import org.apache.servicemix.jbi.management.AttributeInfoHelper;
030 import org.apache.servicemix.jbi.management.MBeanInfoProvider;
031 import org.apache.xbean.classloader.DestroyableClassLoader;
032 import org.apache.xbean.classloader.JarFileClassLoader;
033
034 public class SharedLibrary implements SharedLibraryMBean, MBeanInfoProvider {
035
036 private org.apache.servicemix.jbi.deployment.SharedLibrary library;
037 private File installationDir;
038 private ClassLoader classLoader;
039
040 public SharedLibrary(org.apache.servicemix.jbi.deployment.SharedLibrary library,
041 File installationDir) {
042 this.library = library;
043 this.installationDir = installationDir;
044 this.classLoader = createClassLoader();
045 }
046
047 public void dispose() {
048 if (classLoader instanceof DestroyableClassLoader) {
049 ((DestroyableClassLoader) classLoader).destroy();
050 }
051 classLoader = null;
052 }
053
054 /**
055 * @return the library
056 */
057 public org.apache.servicemix.jbi.deployment.SharedLibrary getLibrary() {
058 return library;
059 }
060
061 public ClassLoader getClassLoader() {
062 return this.classLoader;
063 }
064
065 private ClassLoader createClassLoader() {
066 boolean parentFirst = library.isParentFirstClassLoaderDelegation();
067 // Make the current ClassLoader the parent
068 ClassLoader parent = getClass().getClassLoader();
069
070 ClassPath cp = library.getSharedLibraryClassPath();
071 String[] classPathNames = cp.getPathElements();
072 URL[] urls = new URL[classPathNames.length];
073 for (int i = 0; i < classPathNames.length; i++) {
074 File file = new File(installationDir, classPathNames[i]);
075 try {
076 urls[i] = file.toURL();
077 } catch (MalformedURLException e) {
078 throw new IllegalArgumentException(classPathNames[i], e);
079 }
080 }
081 return new JarFileClassLoader(
082 library.getIdentification().getName(),
083 urls,
084 parent,
085 !parentFirst,
086 new String[0],
087 new String[] {"java.", "javax." });
088 }
089
090 public String getDescription() {
091 return library.getIdentification().getDescription();
092 }
093
094 public String getName() {
095 return library.getIdentification().getName();
096 }
097
098 public String getVersion() {
099 return library.getVersion();
100 }
101
102 public Object getObjectToManage() {
103 return this;
104 }
105
106 public MBeanAttributeInfo[] getAttributeInfos() throws JMException {
107 AttributeInfoHelper helper = new AttributeInfoHelper();
108 helper.addAttribute(getObjectToManage(), "name", "name of the shared library");
109 helper.addAttribute(getObjectToManage(), "description", "description of this shared library");
110 helper.addAttribute(getObjectToManage(), "version", "version of this shared library");
111 return helper.getAttributeInfos();
112 }
113
114 public MBeanOperationInfo[] getOperationInfos() throws JMException {
115 return null;
116 }
117
118 public String getSubType() {
119 return null;
120 }
121
122 public String getType() {
123 return "SharedLibrary";
124 }
125
126 public void setPropertyChangeListener(PropertyChangeListener l) {
127 // We do not fire property events, so need to keep
128 // a reference
129 }
130
131 }