equinox-agentclass-hook/src/main/java/org/netbeans/html/equinox/agentclass/AgentHook.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 400 feb094ada684
permissions -rw-r--r--
Updating copyright headers to mention current year
jaroslav@393
     1
/**
jaroslav@393
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@393
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@393
     5
 *
jaroslav@393
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@393
     7
 * Other names may be trademarks of their respective owners.
jaroslav@393
     8
 *
jaroslav@393
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@393
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@393
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@393
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@393
    13
 * License. You can obtain a copy of the License at
jaroslav@393
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@393
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@393
    16
 * specific language governing permissions and limitations under the
jaroslav@393
    17
 * License.  When distributing the software, include this License Header
jaroslav@393
    18
 * Notice in each file and include the License file at
jaroslav@393
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@393
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@393
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@393
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@393
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@393
    24
 * your own identifying information:
jaroslav@393
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@393
    26
 *
jaroslav@393
    27
 * Contributor(s):
jaroslav@393
    28
 *
jaroslav@393
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jaroslav@393
    31
 *
jaroslav@393
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@393
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@393
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@393
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@393
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@393
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@393
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@393
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@393
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@393
    41
 * made subject to such option by the copyright holder.
jaroslav@393
    42
 */
jaroslav@393
    43
package org.netbeans.html.equinox.agentclass;
jaroslav@393
    44
jaroslav@393
    45
import java.lang.instrument.IllegalClassFormatException;
jaroslav@393
    46
import java.security.ProtectionDomain;
jaroslav@393
    47
import java.util.ArrayList;
jaroslav@400
    48
import java.util.logging.Logger;
jaroslav@393
    49
jaroslav@393
    50
import org.eclipse.osgi.baseadaptor.BaseData;
jaroslav@393
    51
import org.eclipse.osgi.baseadaptor.HookConfigurator;
jaroslav@393
    52
import org.eclipse.osgi.baseadaptor.HookRegistry;
jaroslav@393
    53
import org.eclipse.osgi.baseadaptor.bundlefile.BundleEntry;
jaroslav@393
    54
import org.eclipse.osgi.baseadaptor.hooks.ClassLoadingHook;
jaroslav@393
    55
import org.eclipse.osgi.baseadaptor.loader.BaseClassLoader;
jaroslav@393
    56
import org.eclipse.osgi.baseadaptor.loader.ClasspathEntry;
jaroslav@393
    57
import org.eclipse.osgi.baseadaptor.loader.ClasspathManager;
jaroslav@393
    58
import org.eclipse.osgi.framework.adaptor.BundleProtectionDomain;
jaroslav@393
    59
import org.eclipse.osgi.framework.adaptor.BundleWatcher;
jaroslav@393
    60
import org.eclipse.osgi.framework.adaptor.ClassLoaderDelegate;
jaroslav@393
    61
import org.osgi.framework.Bundle;
jaroslav@393
    62
import org.osgi.framework.BundleContext;
jaroslav@393
    63
import org.osgi.framework.wiring.BundleWiring;
jaroslav@393
    64
jaroslav@393
    65
public class AgentHook implements HookConfigurator, BundleWatcher, ClassLoadingHook {
jaroslav@400
    66
    private static final Logger LOG = Logger.getLogger(AgentHook.class.getName());
jaroslav@393
    67
	private boolean all;
jaroslav@393
    68
	
jaroslav@393
    69
	@Override
jaroslav@393
    70
	public void addHooks(HookRegistry hookRegistry) {
jaroslav@400
    71
		LOG.info("Agent hook for Equinox initialized!");
jaroslav@393
    72
		hookRegistry.addWatcher(this);
jaroslav@393
    73
		hookRegistry.addClassLoadingHook(this);
jaroslav@393
    74
	}
jaroslav@393
    75
jaroslav@393
    76
	@Override
jaroslav@393
    77
	public void watchBundle(Bundle bundle, int type) {
jaroslav@393
    78
		if (!all) {
jaroslav@393
    79
			BundleContext c = bundle.getBundleContext();
jaroslav@393
    80
			if (c != null) {
jaroslav@393
    81
				Bundle[] arr = bundle.getBundleContext().getBundles();
jaroslav@393
    82
				for (Bundle b : arr) {
jaroslav@393
    83
					agentBundle(b);
jaroslav@393
    84
				}
jaroslav@393
    85
				all = true;
jaroslav@393
    86
			}
jaroslav@393
    87
		}
jaroslav@393
    88
		if (type == BundleWatcher.END_ACTIVATION) {
jaroslav@393
    89
			agentBundle(bundle);
jaroslav@393
    90
		}
jaroslav@393
    91
	}
jaroslav@393
    92
jaroslav@393
    93
	private void agentBundle(Bundle bundle) {
jaroslav@393
    94
		String agentClass = (String)bundle.getHeaders().get("Agent-Class");
jaroslav@393
    95
		if (agentClass != null) {
jaroslav@393
    96
			Class<?> agent;
jaroslav@393
    97
			try {
jaroslav@393
    98
				agent = bundle.loadClass(agentClass);
jaroslav@393
    99
				NbInstrumentation.registerAgent(agent.getClassLoader(), agent.getName());
jaroslav@393
   100
			} catch (ClassNotFoundException e) {
jaroslav@393
   101
				throw new IllegalStateException(e);
jaroslav@393
   102
			}
jaroslav@393
   103
		}
jaroslav@393
   104
	}
jaroslav@393
   105
jaroslav@393
   106
	@Override
jaroslav@393
   107
	public byte[] processClass(String name, byte[] bytes,
jaroslav@393
   108
			ClasspathEntry ce, BundleEntry entry,
jaroslav@393
   109
			ClasspathManager manager) {
jaroslav@393
   110
        final BaseData bd = ce.getBaseData();
jaroslav@393
   111
        if (bd == null) {
jaroslav@393
   112
            return bytes;
jaroslav@393
   113
        }
jaroslav@393
   114
        final Bundle b = bd.getBundle();
jaroslav@393
   115
        if (b == null) {
jaroslav@393
   116
            return bytes;
jaroslav@393
   117
        }
jaroslav@393
   118
        BundleWiring w = (BundleWiring)b.adapt(BundleWiring.class);
jaroslav@393
   119
        if (w == null) {
jaroslav@393
   120
            return bytes;
jaroslav@393
   121
        }
jaroslav@393
   122
        ClassLoader loader = w.getClassLoader();
jaroslav@393
   123
		try {
jaroslav@393
   124
			return NbInstrumentation.patchByteCode(loader, name, ce.getDomain(), bytes);
jaroslav@393
   125
		} catch (IllegalClassFormatException e) {
jaroslav@393
   126
			return bytes;
jaroslav@393
   127
		}
jaroslav@393
   128
	}
jaroslav@393
   129
jaroslav@393
   130
	@Override
jaroslav@393
   131
	public boolean addClassPathEntry(ArrayList cpEntries,
jaroslav@393
   132
			String cp, ClasspathManager hostmanager, BaseData sourcedata,
jaroslav@393
   133
			ProtectionDomain sourcedomain) {
jaroslav@393
   134
		// TODO Auto-generated method stub
jaroslav@393
   135
		return false;
jaroslav@393
   136
	}
jaroslav@393
   137
jaroslav@393
   138
	@Override
jaroslav@393
   139
	public String findLibrary(BaseData data, String libName) {
jaroslav@393
   140
		// TODO Auto-generated method stub
jaroslav@393
   141
		return null;
jaroslav@393
   142
	}
jaroslav@393
   143
jaroslav@393
   144
	@Override
jaroslav@393
   145
	public ClassLoader getBundleClassLoaderParent() {
jaroslav@393
   146
		// TODO Auto-generated method stub
jaroslav@393
   147
		return null;
jaroslav@393
   148
	}
jaroslav@393
   149
jaroslav@393
   150
	@Override
jaroslav@393
   151
	public BaseClassLoader createClassLoader(ClassLoader parent,
jaroslav@393
   152
			ClassLoaderDelegate delegate, BundleProtectionDomain domain,
jaroslav@393
   153
			BaseData data, String[] bundleclasspath) {
jaroslav@393
   154
		// TODO Auto-generated method stub
jaroslav@393
   155
		return null;
jaroslav@393
   156
	}
jaroslav@393
   157
jaroslav@393
   158
	@Override
jaroslav@393
   159
	public void initializedClassLoader(BaseClassLoader baseClassLoader,
jaroslav@393
   160
			BaseData data) {
jaroslav@393
   161
		// TODO Auto-generated method stub
jaroslav@393
   162
		
jaroslav@393
   163
	}
jaroslav@393
   164
}