Skip to main content
summaryrefslogtreecommitdiffstats
blob: de3f02a6aeec48214fda5f8ce75bd5df8ab248c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*******************************************************************************
 * Copyright (c) 2006, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *   David Knibb               initial implementation      
 *   Matthew Webster           Eclipse 3.2 changes 
 *   Martin Lippert            supplementing mechanism reworked     
 *******************************************************************************/

package org.eclipse.equinox.weaving.hooks;

import java.io.IOException;
import java.net.URL;

import org.eclipse.equinox.weaving.adaptors.AspectJAdaptor;
import org.eclipse.equinox.weaving.adaptors.AspectJAdaptorFactory;
import org.eclipse.equinox.weaving.adaptors.Debug;
import org.eclipse.equinox.weaving.adaptors.IAspectJAdaptor;
import org.eclipse.osgi.baseadaptor.BaseData;
import org.eclipse.osgi.baseadaptor.bundlefile.BundleEntry;
import org.eclipse.osgi.baseadaptor.bundlefile.BundleFile;
import org.eclipse.osgi.baseadaptor.loader.BaseClassLoader;
import org.eclipse.osgi.baseadaptor.loader.ClasspathEntry;
import org.eclipse.osgi.baseadaptor.loader.ClasspathManager;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.ServiceReference;
import org.osgi.service.packageadmin.PackageAdmin;

public class AspectJHook extends AbstractAspectJHook {

    private BundleContext bundleContext;

    private AspectJAdaptorFactory adaptorFactory;

    public AspectJHook() {
        if (Debug.DEBUG_GENERAL) Debug.println("- AspectJHook.<init>()");
        adaptorFactory = new AspectJAdaptorFactory();
    }

    private void initialize(BundleContext context) {
        if (Debug.DEBUG_GENERAL)
            Debug.println("> AspectJHook.initialize() context=" + context);

        this.bundleContext = context;
        adaptorFactory.initialize(context, getSupplementerRegistry());

        ServiceReference serviceReference = context
                .getServiceReference(PackageAdmin.class.getName());
        PackageAdmin packageAdmin = (PackageAdmin) context
                .getService(serviceReference);

        getSupplementerRegistry().setBundleContext(context);
        getSupplementerRegistry().setPackageAdmin(packageAdmin);
        context.addBundleListener(new SupplementBundleListener(
                getSupplementerRegistry()));

        if (Debug.DEBUG_GENERAL)
            Debug.println("< AspectJHook.initialize() adaptorFactory="
                    + adaptorFactory);
    }

    public void frameworkStart(BundleContext context) throws BundleException {
        //		Debug.println("? AspectJHook.frameworkStart() context=" + context + ", fdo=" + FrameworkDebugOptions.getDefault());
        initialize(context);
    }

    public void recordClassDefine(String name, Class clazz, byte[] classbytes,
            ClasspathEntry classpathEntry, BundleEntry entry,
            ClasspathManager manager) {
        if (entry instanceof AspectJBundleEntry) {
            AspectJBundleEntry ajBundleEntry = (AspectJBundleEntry) entry;
            if (!ajBundleEntry.dontWeave()) {
                IAspectJAdaptor adaptor = ajBundleEntry.getAdaptor();
                URL sourceFileURL = ajBundleEntry.getBundleFileURL();
                adaptor.storeClass(name, sourceFileURL, clazz, classbytes);
            }
        }
    }

    public void initializedClassLoader(BaseClassLoader baseClassLoader,
            BaseData data) {
        if (Debug.DEBUG_GENERAL)
            Debug
                    .println("> AspectJHook.initializedClassLoader() bundle="
                            + data.getSymbolicName() + ", loader="
                            + baseClassLoader + ", data=" + data
                            + ", bundleFile=" + data.getBundleFile());
        IAspectJAdaptor adaptor = null;
        BundleFile bundleFile = data.getBundleFile();
        if (bundleFile instanceof BaseAjBundleFile) {
            BaseAjBundleFile baseBundleFile = (BaseAjBundleFile) bundleFile;
            adaptor = baseBundleFile.getAdaptor();
            adaptor.setBaseClassLoader(baseClassLoader);
        }

        if (Debug.DEBUG_GENERAL)
            Debug.println("< AspectJHook.initializedClassLoader() adaptor="
                    + adaptor);
    }

    public byte[] processClass(String name, byte[] classbytes,
            ClasspathEntry classpathEntry, BundleEntry entry,
            ClasspathManager manager) {
        byte[] newClassytes = null;
        if (entry instanceof AspectJBundleEntry) {
            AspectJBundleEntry ajBundleEntry = (AspectJBundleEntry) entry;
            if (!ajBundleEntry.dontWeave()) {
                IAspectJAdaptor adaptor = ajBundleEntry.getAdaptor();
                newClassytes = adaptor.weaveClass(name, classbytes);
            }
        }
        return newClassytes;
    }

    public BundleFile wrapBundleFile(BundleFile bundleFile, Object content,
            BaseData data, boolean base) throws IOException {
        BundleFile wrapped = null;
        if (Debug.DEBUG_BUNDLE)
            Debug
                    .println("> AspectJBundleFileWrapperFactoryHook.wrapBundleFile() bundle="
                            + data.getSymbolicName()
                            + " bundleFile="
                            + bundleFile
                            + ", content="
                            + content
                            + ", data="
                            + data
                            + ", base="
                            + base
                            + ", baseFile="
                            + bundleFile.getBaseFile());

        if (base) {
            IAspectJAdaptor adaptor = createAspectJAdaptor(data);
            if (adaptor != null) {
                wrapped = new BaseAjBundleFile(adaptor, bundleFile);
            }
        } else {
            IAspectJAdaptor adaptor = null;
            if (bundleContext != null) {
                adaptor = getAspectJAdaptor(data);
                if (Debug.DEBUG_BUNDLE)
                    Debug
                            .println("- AspectJBundleFileWrapperFactoryHook.wrapBundleFile() adaptor="
                                    + adaptor);
                //				if (adaptor == null) throw new RuntimeException(data.getSymbolicName());
            }
            if (adaptor != null) {
                wrapped = new AspectJBundleFile(adaptor, bundleFile);
            }
        }
        if (Debug.DEBUG_BUNDLE)
            Debug
                    .println("< AspectJBundleFileWrapperFactoryHook.wrapBundleFile() wrapped="
                            + wrapped);
        return wrapped;
    }

    private IAspectJAdaptor createAspectJAdaptor(BaseData baseData) {
        if (Debug.DEBUG_GENERAL)
            Debug.println("> AspectJHook.createAspectJAdaptor() location="
                    + baseData.getLocation());
        IAspectJAdaptor adaptor = null;

        if (adaptorFactory != null) {
            adaptor = new AspectJAdaptor(baseData, adaptorFactory, null, null,
                    null);
        } else {
            if (Debug.DEBUG_GENERAL)
                Debug.println("- AspectJHook.createAspectJAdaptor() factory="
                        + adaptorFactory);
        }

        if (Debug.DEBUG_GENERAL)
            Debug.println("< AspectJHook.createAspectJAdaptor() adaptor="
                    + adaptor);
        return adaptor;
    }

    private IAspectJAdaptor getAspectJAdaptor(BaseData data) {
        IAspectJAdaptor adaptor = null;

        BundleFile bundleFile = data.getBundleFile();
        if (bundleFile instanceof BaseAjBundleFile) {
            BaseAjBundleFile baseBundleFile = (BaseAjBundleFile) bundleFile;
            adaptor = baseBundleFile.getAdaptor();
        }

        return adaptor;
    }

}

Back to the top