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:
018: /**
019: * @author Ivan Popov
020: * @version $Revision$
021: */package org.apache.harmony.tools.internal.jdi;
022:
023: import com.sun.jdi.VirtualMachineManager;
024: import java.net.URLClassLoader;
025: import java.net.URL;
026: import java.util.ArrayList;
027: import java.util.MissingResourceException;
028: import java.util.ResourceBundle;
029:
030: /**
031: * This class is a proxy for JDI bootstrap implementation.
032: *
033: * It looks for corresponding jars with JDI implementation, loads appropriate bootstrap class
034: * and redirects initialization to it. Details of the used JDI implementation are specified in
035: * Bootstrap.properties file.
036: */
037: public class Bootstrap {
038:
039: /**
040: * Default name for JDI properties bundle.
041: */
042: public static final String BUNDLE_JDI_NAME = "org.apache.harmony.tools.internal.jdi.Bootstrap";
043:
044: /**
045: * Property to specify name of JDI Bootstrap class.
046: */
047: public static final String PROPERTY_JDI_BOOTSTRAP = "org.apache.harmony.tools.jdi.bootstrap";
048:
049: /**
050: * Property to specify location of jars with JDI implementation.
051: */
052: public static final String PROPERTY_JDI_LOCATION = "org.apache.harmony.tools.jdi.location";
053:
054: /**
055: * Property prefix to specify name of jars with JDI implementation.
056: */
057: public static final String PROPERTY_JDI_JAR = "org.apache.harmony.tools.jdi.jar.";
058:
059: /**
060: * Loads JDI implementation from corresponding jars and redirects initialization to
061: * its Bootstrap class.
062: *
063: * @return instance of VirtualMachineManager created by JDI implementation
064: * @throw RuntimeException if there is any problem in locating and loading JDI implementation
065: */
066: static public VirtualMachineManager virtualMachineManager() {
067: if (vmm == null) {
068: String bootstrapName = null;
069: String baseDir = null;
070: try {
071: // load resource bundle with JDI implementation properties
072: ResourceBundle bundle = ResourceBundle
073: .getBundle(BUNDLE_JDI_NAME);
074:
075: // read JDI properties from resource bundle
076: bootstrapName = bundle
077: .getString(PROPERTY_JDI_BOOTSTRAP);
078:
079: try {
080: baseDir = bundle.getString(PROPERTY_JDI_LOCATION);
081: } catch (MissingResourceException e) {
082: // ignore exception and use default location
083: baseDir = System.getProperty("java.home")
084: + "/../lib";
085: }
086:
087: ArrayList urls = new ArrayList();
088: try {
089: for (int i = 0;; i++) {
090: String key = PROPERTY_JDI_JAR + i;
091: String jar = bundle.getString(key);
092: URL url = new URL("file", null, baseDir + "/"
093: + jar);
094: urls.add(url);
095: }
096: } catch (MissingResourceException e) {
097: // ignore exception and finish reading jar entries
098: }
099:
100: // create class loader for JDI implementation
101: URL buf[] = new URL[urls.size()];
102: ClassLoader loader = new URLClassLoader((URL[]) (urls
103: .toArray(buf)));
104:
105: // load and initialize JDI bootsrap
106: Class cls = Class.forName(bootstrapName, true, loader);
107: java.lang.reflect.Method method = cls.getMethod(
108: "virtualMachineManager", null);
109: vmm = (VirtualMachineManager) method.invoke(null, null);
110: } catch (Exception e) {
111: throw new RuntimeException(
112: "Cannot initialize JDI bootstrap: "
113: + bootstrapName, e);
114: }
115: }
116: return vmm;
117: }
118:
119: static private VirtualMachineManager vmm = null;
120: }
|