Skip to main content
summaryrefslogtreecommitdiffstats
blob: 983e19495dc99cc279126b70aef75f642c69a40b (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*******************************************************************************
 * Copyright (c) 2009 Martin Lippert 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:
 *   Martin Lippert               initial implementation
 *******************************************************************************/

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

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.aspectj.weaver.loadtime.definition.Definition;
import org.eclipse.equinox.service.weaving.ISupplementerRegistry;
import org.eclipse.equinox.weaving.aspectj.AspectAdmin;
import org.eclipse.equinox.weaving.aspectj.AspectConfiguration;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.osgi.service.resolver.ExportPackageDescription;
import org.eclipse.osgi.service.resolver.State;
import org.eclipse.osgi.util.ManifestElement;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;

/**
 * The aspect resolver is responsible for finding the right connections between
 * bundles and aspects. It calculates the set of aspects that should be woven
 * into a specific bundle, depending on the resolved wires of the bundle and the
 * aspects declared by the wired bundles.
 * 
 * @author Martin Lippert
 */
public class AspectResolver {

    private final AspectAdmin aspectAdmin;

    private final State state;

    private final ISupplementerRegistry supplementerRegistry;

    private final BundleContext weavingBundleContext;

    /**
     * Creates a new instance of an aspect resolver for the given state. This
     * resolver can be used to resolve aspect configurations for bundles. It
     * does not carry any own state, it just uses the injected components to
     * retrieve the necessary informaton to determine which aspects to weave
     * into which bundles.
     * 
     * @param state The state of the OSGi framework (contains wiring
     *            information)
     * @param supplementerRegistry The supplementer registry
     * @param aspectAdmin The aspect admin, which tells us which bundle exports
     *            which aspects
     * @param bundleContext The bundle context in which the aspect resolver is
     *            used
     */
    public AspectResolver(final State state,
            final ISupplementerRegistry supplementerRegistry,
            final AspectAdmin aspectAdmin, final BundleContext bundleContext) {
        this.state = state;
        this.supplementerRegistry = supplementerRegistry;
        this.aspectAdmin = aspectAdmin;
        this.weavingBundleContext = bundleContext;
    }

    /**
     * Resolve the aspects to be woven into the given bundle
     * 
     * @param bundle The bundle in which the aspects should be woven into
     * @param bundleDescription The description of the bundle to be woven into
     * @return The configuration of aspects what should be woven into the bundle
     */
    public AspectConfiguration resolveAspectsFor(final Bundle bundle,
            final BundleDescription bundleDescription) {
        final List<String> fingerprintElements = new ArrayList<String>();

        final List<Definition> definitions = resolveAspectsForBundle(
                fingerprintElements, bundle, bundleDescription);

        final Definition[] foundDefinitions = definitions
                .toArray(new Definition[definitions.size()]);

        Collections.sort(fingerprintElements);
        final StringBuilder fingerprint = new StringBuilder();
        final Iterator<String> iterator = fingerprintElements.iterator();
        while (iterator.hasNext()) {
            final String element = iterator.next();
            fingerprint.append(element);
            fingerprint.append(';');
        }

        return new AspectConfiguration(bundle, foundDefinitions, fingerprint
                .toString());
    }

    private int getApplyAspectsPolicy(final ManifestElement[] headers,
            final String manifestValue) {
        int result = AspectAdmin.ASPECT_APPLY_POLICY_NOT_DEFINED;

        if (headers != null) {
            for (int i = 0; i < headers.length; i++) {
                if (headers[i].getValue().equals(manifestValue)) {
                    final String directive = headers[i]
                            .getDirective(AspectAdmin.ASPECT_APPLY_POLICY_DIRECTIVE);
                    if ("true".equals(directive)) { //$NON-NLS-1$
                        result = AspectAdmin.ASPECT_APPLY_POLICY_TRUE;
                    } else if ("false".equals(directive)) { //$NON-NLS-1$
                        result = AspectAdmin.ASPECT_APPLY_POLICY_FALSE;
                    }
                }
            }
        }
        return result;
    }

    private String getBundleVersion(final Bundle bundle) {
        return state.getBundle(bundle.getBundleId()).getVersion().toString();
    }

    private List<Definition> resolveAspectsForBundle(
            final List<String> fingerprintElements, final Bundle bundle,
            final BundleDescription bundleDescription) {
        final List<Definition> result = new ArrayList<Definition>();

        if (weavingBundleContext != null) {

            Definition aspects = null;

            // fragments
            final BundleDescription[] fragments = bundleDescription
                    .getFragments();
            for (int i = 0; i < fragments.length; i++) {
                final Bundle fragmentBundle = weavingBundleContext
                        .getBundle(fragments[i].getBundleId());
                if (fragmentBundle != null) {
                    aspects = aspectAdmin.getAspectDefinition(fragmentBundle);
                    if (aspects != null) {
                        result.add(aspects);
                        fingerprintElements.add(fragmentBundle
                                .getSymbolicName()
                                + ":" //$NON-NLS-1$
                                + fragments[i].getVersion().toString());
                    }
                }
            }

            // required bundles
            final BundleDescription[] resolvedRequires = bundleDescription
                    .getResolvedRequires();
            ManifestElement[] requireHeaders = null;
            if (resolvedRequires.length > 0) {
                try {
                    requireHeaders = ManifestElement
                            .parseHeader(Constants.REQUIRE_BUNDLE,
                                    (String) bundle.getHeaders().get(
                                            Constants.REQUIRE_BUNDLE));
                } catch (final BundleException e) {
                }
            }
            for (int i = 0; i < resolvedRequires.length; i++) {
                final Bundle requiredBundle = weavingBundleContext
                        .getBundle(resolvedRequires[i].getBundleId());
                if (requiredBundle != null) {
                    final int applyPolicy = getApplyAspectsPolicy(
                            requireHeaders, requiredBundle.getSymbolicName());

                    aspects = aspectAdmin.resolveRequiredBundle(requiredBundle,
                            applyPolicy);

                    if (aspects != null) {
                        result.add(aspects);
                        fingerprintElements.add(requiredBundle
                                .getSymbolicName()
                                + ":" //$NON-NLS-1$
                                + resolvedRequires[i].getVersion().toString());
                    }
                }
            }

            // imported packages
            final ExportPackageDescription[] resolvedImports = bundleDescription
                    .getResolvedImports();
            ManifestElement[] importHeaders = null;
            if (resolvedImports.length > 0) {
                try {
                    importHeaders = ManifestElement
                            .parseHeader(Constants.IMPORT_PACKAGE,
                                    (String) bundle.getHeaders().get(
                                            Constants.IMPORT_PACKAGE));
                } catch (final BundleException e) {
                }
            }
            for (int i = 0; i < resolvedImports.length; i++) {
                final Bundle exportingBundle = weavingBundleContext
                        .getBundle(resolvedImports[i].getExporter()
                                .getBundleId());
                if (exportingBundle != null) {
                    final String importedPackage = resolvedImports[i].getName();

                    final int applyPolicy = getApplyAspectsPolicy(
                            importHeaders, importedPackage);

                    aspects = aspectAdmin.resolveImportedPackage(
                            exportingBundle, importedPackage, applyPolicy);

                    if (aspects != null) {
                        result.add(aspects);
                        fingerprintElements.add(importedPackage + ":" //$NON-NLS-1$
                                + resolvedImports[i].getVersion().toString());
                    }
                }
            }

            // supplementers
            final Bundle[] supplementers = this.supplementerRegistry
                    .getSupplementers(bundleDescription.getBundleId());

            for (int i = 0; i < supplementers.length; i++) {
                aspects = aspectAdmin
                        .getExportedAspectDefinitions(supplementers[i]);
                if (aspects != null) {
                    result.add(aspects);
                    fingerprintElements.add(supplementers[i].getSymbolicName()
                            + ":" //$NON-NLS-1$
                            + getBundleVersion(supplementers[i]));
                }
            }

            // this bundle
            if (result.size() > 0) {
                aspects = aspectAdmin.getAspectDefinition(bundle);
                if (aspects != null) {
                    result.add(aspects);
                    fingerprintElements.add(bundle.getSymbolicName() + ":" //$NON-NLS-1$
                            + bundleDescription.getVersion().toString());
                }
            }

        }

        return result;
    }

}

Back to the top