Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9534299f07cc12a8978eca3b8fee7e5406d34dd2 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*******************************************************************************
 * Copyright (c) 2006 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
 *   Heiko Seeberger           AJDT 1.5.1 changes
 *   Martin Lippert            minor changes and bugfixes
 *******************************************************************************/

package org.eclipse.equinox.weaving.aspectj.loadtime;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

import org.aspectj.weaver.loadtime.ClassLoaderWeavingAdaptor;
import org.aspectj.weaver.loadtime.definition.DocumentParser;
import org.eclipse.equinox.weaving.aspectj.WeavingServicePlugin;
import org.osgi.framework.Bundle;

public class OSGiWeavingAdaptor extends ClassLoaderWeavingAdaptor {

    private static final String AOP_CONTEXT_LOCATION_HEADER = "Eclipse-AspectContext";

    private static final String DEFAULT_AOP_CONTEXT_LOCATION = "META-INF/aop.xml";

    private boolean initialized;

    private boolean initializing;

    private ClassLoader classLoader;

    private OSGiWeavingContext weavingContext;

    private StringBuffer namespaceAddon;

    public OSGiWeavingAdaptor(ClassLoader loader, OSGiWeavingContext context) {
        super();
        this.classLoader = loader;
        this.weavingContext = context;
        this.namespaceAddon = new StringBuffer();
    }

    // Bug 215177: Adapt to updated (AJ 1.5.4) super class signature:
    /**
     * @see org.aspectj.weaver.tools.WeavingAdaptor#weaveClass(java.lang.String,
     *      byte[], boolean)
     */
    public byte[] weaveClass(String name, byte[] bytes, boolean mustWeave)
            throws IOException {

        /* Avoid recursion during adaptor initialization */
        if (!initializing) {
            if (!initialized) {
                initializing = true;
                initialize(classLoader, weavingContext);
                initialized = true;
                initializing = false;
            }
            // Bug 215177: Adapt to updated (AJ 1.5.4) super class signature:
            bytes = super.weaveClass(name, bytes, mustWeave);
        }
        return bytes;
    }

    public void initialize() {
        if (!initializing) {
            if (!initialized) {
                initializing = true;
                super.initialize(classLoader, weavingContext);
                initialized = true;
                initializing = false;
            }
        }

        if (WeavingServicePlugin.verbose) {
            if (isEnabled())
                System.err
                        .println("[org.aspectj.osgi.service.weaving] info weaving bundle '"
                                + weavingContext.getClassLoaderName() + "'");
            else
                System.err
                        .println("[org.aspectj.osgi.service.weaving] info not weaving bundle '"
                                + weavingContext.getClassLoaderName() + "'");
        }
    }

    /**
     * Load and cache the aop.xml/properties according to the classloader
     * visibility rules
     * 
     * @param weaver
     * @param loader
     */
    public List parseDefinitionsForBundle() {
        List definitions = new ArrayList();
        Set seenBefore = new HashSet();

        try {
            parseDefinitionsFromVisibility(definitions, seenBefore);
            parseDefinitionsFromRequiredBundles(definitions, seenBefore);
            if (definitions.isEmpty()) {
                info("no configuration found. Disabling weaver for bundler "
                        + weavingContext.getClassLoaderName());
            }
        } catch (Exception e) {
            definitions.clear();
            warn("parse definitions failed", e);
        }

        return definitions;
    }

    private void parseDefinitionsFromVisibility(List definitions, Set seenBefore) {
        String resourcePath = System.getProperty(
                "org.aspectj.weaver.loadtime.configuration", "");
        StringTokenizer st = new StringTokenizer(resourcePath, ";");

        while (st.hasMoreTokens()) {
            try {
                Enumeration xmls = weavingContext.getResources(st.nextToken());

                while (xmls.hasMoreElements()) {
                    URL xml = (URL) xmls.nextElement();
                    if (!seenBefore.contains(xml)) {
                        info("using configuration "
                                + weavingContext.getFile(xml));
                        definitions.add(DocumentParser.parse(xml));
                        seenBefore.add(xml);

                        addToNamespaceAddon(xml);
                    } else {
                        warn("ignoring duplicate definition: " + xml);
                    }
                }
            } catch (Exception e) {
                warn("parse definitions failed", e);
            }
        }
    }

    private void addToNamespaceAddon(URL xml) {
        String bundleName = weavingContext.getBundleIdFromURL(xml);
        String bundleVersion = weavingContext.getBundleVersionFromURL(xml);

        namespaceAddon.append(bundleName);
        namespaceAddon.append(":");
        namespaceAddon.append(bundleVersion);
        namespaceAddon.append(";");
    }

    private void parseDefinitionsFromRequiredBundles(List definitions,
            Set seenBefore) {
        Bundle[] bundles = weavingContext.getBundles();

        Arrays.sort(bundles, new Comparator() {

            public int compare(Object arg0, Object arg1) {
                long bundleId1 = ((Bundle) arg0).getBundleId();
                long bundleId2 = ((Bundle) arg1).getBundleId();
                return (int) (bundleId1 - bundleId2);
            }
        });

        for (int i = 0; i < bundles.length; i++) {
            parseDefinitionFromRequiredBundle(bundles[i], definitions,
                    seenBefore);
        }
    }

    private void parseDefinitionFromRequiredBundle(Bundle bundle,
            List definitions, Set seenBefore) {
        try {
            URL aopXmlDef = bundle.getEntry(getDefinitionLocation(bundle));
            if (aopXmlDef != null) {
                if (!seenBefore.contains(aopXmlDef)) {
                    definitions.add(DocumentParser.parse(aopXmlDef));
                    seenBefore.add(aopXmlDef);

                    addToNamespaceAddon(aopXmlDef);
                } else {
                    warn("ignoring duplicate definition: " + aopXmlDef);
                }
            }
        } catch (Exception e) {
            warn("parse definitions failed", e);
        }
    }

    private String getDefinitionLocation(Bundle bundle) {
        String aopContextHeader = (String) bundle.getHeaders().get(
                AOP_CONTEXT_LOCATION_HEADER);
        if (aopContextHeader != null) {
            aopContextHeader = aopContextHeader.trim();
            return aopContextHeader;
        }

        return DEFAULT_AOP_CONTEXT_LOCATION;
    }

    /**
     * @see org.aspectj.weaver.loadtime.ClassLoaderWeavingAdaptor#getNamespace()
     */
    public String getNamespace() {
        String namespace = super.getNamespace();
        if (namespace != null && namespace.length() > 0
                && namespaceAddon.length() > 0) {
            return namespace + " - " + namespaceAddon.toString(); //$NON-NLS-1$
        } else {
            return namespace;
        }
    }

}

Back to the top