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: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.server;
042:
043: import java.io.File;
044: import java.lang.instrument.Instrumentation;
045: import java.net.URI;
046: import java.net.URISyntaxException;
047: import java.net.URL;
048:
049: /**
050: * Class that contains the premain() method, needed by the java.lang.instrument Java agent
051: * mechanism, that we use for "attach on startup" operation with JDK 1.5.
052: *
053: * @author Tomas Hurka
054: * @author Misha Dmitriev
055: */
056: public class ProfilerActivate15 {
057: //~ Methods ------------------------------------------------------------------------------------------------------------------
058:
059: public static void agentmain(final String agentArgs,
060: final Instrumentation inst) {
061: activate(agentArgs, inst, ProfilerServer.ATTACH_DYNAMIC);
062: }
063:
064: /**
065: * This method is called after the VM has been initialized, but before the TA's main() method.
066: * A single arguments string passed to it is the "options" string specified to the -javaagent
067: * argument, as java -javaagent:jarpath=options. It should contain the communication port number
068: * and optional timeout separated by a comma.
069: */
070: public static void premain(String agentArgs, Instrumentation inst) {
071: activate(agentArgs, inst, ProfilerServer.ATTACH_DIRECT);
072: }
073:
074: private static File getArchiveFile(URL url) {
075: String protocol = url.getProtocol();
076:
077: if ("jar".equals(protocol)) { //NOI18N
078:
079: String path = url.getPath();
080: int index = path.indexOf("!/"); //NOI18N
081:
082: if (index >= 0) {
083: try {
084: return new File(new URI(path.substring(0, index)));
085: } catch (URISyntaxException ex) {
086: throw new IllegalArgumentException(url.toString());
087: }
088: }
089: }
090:
091: throw new IllegalArgumentException(url.toString());
092: }
093:
094: private static void activate(String agentArgs,
095: Instrumentation inst, int activateCode) {
096: URL classUrl = ClassLoader
097: .getSystemClassLoader()
098: .getResource(
099: "org/netbeans/lib/profiler/server/ProfilerActivate15.class");
100: File jar = getArchiveFile(classUrl);
101: String fullJFluidPath = jar.getParent();
102:
103: if ((agentArgs == null) || (agentArgs.length() == 0)) { // no options, just load the native library. This is used for remote-pack calibration
104: ProfilerServer.loadNativeLibrary(fullJFluidPath, false);
105:
106: return;
107: }
108:
109: int timeOut = 0;
110: int commaPos = agentArgs.indexOf(',');
111:
112: if (commaPos != -1) { // optional timeout is specified
113:
114: String timeOutStr = agentArgs.substring(commaPos + 1,
115: agentArgs.length());
116:
117: try {
118: timeOut = Integer.parseInt(timeOutStr);
119: } catch (NumberFormatException ex) {
120: System.err
121: .println("*** Profiler Engine: invalid timeout number specified to premain(): "
122: + timeOutStr); // NOI18N
123: System.exit(-1);
124: }
125:
126: agentArgs = agentArgs.substring(0, commaPos);
127: }
128:
129: String portStr = agentArgs;
130: int portNo = 0;
131:
132: try {
133: portNo = Integer.parseInt(portStr);
134: } catch (NumberFormatException ex) {
135: System.err
136: .println("*** Profiler Engine: invalid port number specified to premain(): "
137: + portStr); // NOI18N
138: System.exit(-1);
139: }
140:
141: ProfilerServer.activate(fullJFluidPath, portNo, activateCode,
142: timeOut);
143: }
144: }
|