Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3fba382b95a8bdfeba20f0893d587740f33ec187 (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
/*******************************************************************************
 * 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           Enhancements for service dynamics     
 *******************************************************************************/

package org.eclipse.equinox.weaving.adaptors;

import java.util.Iterator;

import org.eclipse.equinox.service.weaving.ICachingService;
import org.eclipse.equinox.service.weaving.IWeavingService;
import org.eclipse.equinox.service.weaving.SupplementerRegistry;
import org.eclipse.osgi.baseadaptor.BaseData;
import org.eclipse.osgi.baseadaptor.loader.BaseClassLoader;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.osgi.service.resolver.State;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.service.packageadmin.PackageAdmin;
import org.osgi.util.tracker.ServiceTracker;

public class AspectJAdaptorFactory {

    private BundleContext bundleContext;

    private ServiceTracker cachingServiceTracker;

    private PackageAdmin packageAdminService;

    private SupplementerRegistry supplementerRegistry;

    private ServiceListener weavingServiceListener;

    private ServiceTracker weavingServiceTracker;

    public AspectJAdaptorFactory() {
    }

    public void dispose(final BundleContext context) {

        context.removeServiceListener(weavingServiceListener);
        if (Debug.DEBUG_WEAVE)
            Debug.println("> Removed service listener for weaving service.");

        weavingServiceTracker.close();
        if (Debug.DEBUG_WEAVE)
            Debug.println("> Closed service tracker for weaving service.");

        cachingServiceTracker.close();
        if (Debug.DEBUG_CACHE)
            Debug.println("> Closed service tracker for caching service.");
    }

    public Bundle getHost(final Bundle fragment) {
        if (Debug.DEBUG_GENERAL)
            Debug.println("> AspectJAdaptorFactory.getHost() fragment="
                    + fragment);

        Bundle host = null;
        if (packageAdminService != null)
            host = packageAdminService.getHosts(fragment)[0];

        if (Debug.DEBUG_GENERAL)
            Debug.println("< AspectJAdaptorFactory.getHost() " + host);
        return host;
    }

    public void initialize(final BundleContext context,
            final SupplementerRegistry supplementerRegistry) {
        if (Debug.DEBUG_GENERAL)
            Debug.println("> AspectJAdaptorFactory.initialize() context="
                    + context);
        this.bundleContext = context;
        this.supplementerRegistry = supplementerRegistry;

        // Service tracker for weaving service
        weavingServiceTracker = new ServiceTracker(context,
                IWeavingService.class.getName(), null);
        weavingServiceTracker.open();
        if (Debug.DEBUG_WEAVE)
            Debug.println("> Opened service tracker for weaving service.");

        // Service listener for weaving service
        weavingServiceListener = new ServiceListener() {

            public void serviceChanged(final ServiceEvent event) {
                if (event.getType() != ServiceEvent.MODIFIED) {
                    final Iterator supplementedBundlesIterator = supplementerRegistry
                            .getSupplementedBundles().iterator();
                    while (supplementedBundlesIterator.hasNext()) {
                        final Bundle supplementedBundle = (Bundle) supplementedBundlesIterator
                                .next();
                        supplementerRegistry
                                .updateInstalledBundle(supplementedBundle);
                        if (Debug.DEBUG_WEAVE)
                            Debug.println("> Updated supplemented bundle "
                                    + supplementedBundle.getSymbolicName());
                    }
                }
            }
        };
        try {
            context.addServiceListener(weavingServiceListener, "("
                    + Constants.OBJECTCLASS + "="
                    + IWeavingService.class.getName() + ")");
        } catch (final InvalidSyntaxException e) { // This is correct!
        }

        // Service tracker for caching service
        cachingServiceTracker = new ServiceTracker(context,
                ICachingService.class.getName(), null);
        cachingServiceTracker.open();
        if (Debug.DEBUG_CACHE)
            Debug.println("> Opened service tracker for caching service.");

        initializePackageAdminService(context);
    }

    protected ICachingService getCachingService(final BaseClassLoader loader,
            final Bundle bundle, final IWeavingService weavingService) {
        if (Debug.DEBUG_CACHE)
            Debug.println("> AspectJAdaptorFactory.getCachingService() bundle="
                    + bundle + ", weavingService=" + weavingService);
        ICachingService service = null;
        String key = "";

        if (weavingService != null) {
            key = weavingService.getKey();
        }
        final ICachingService singletonCachingService = (ICachingService) cachingServiceTracker
                .getService();
        if (singletonCachingService != null) {
            service = singletonCachingService.getInstance((ClassLoader) loader,
                    bundle, key);
        }
        if (Debug.DEBUG_CACHE)
            Debug
                    .println("< AspectJAdaptorFactory.getCachingService() service="
                            + service + ", key='" + key + "'");
        return service;
    }

    protected IWeavingService getWeavingService(final BaseClassLoader loader) {
        if (Debug.DEBUG_WEAVE)
            Debug
                    .println("> AspectJAdaptorFactory.getWeavingService() baseClassLoader="
                            + loader);

        IWeavingService weavingService = null;
        final IWeavingService singletonWeavingService = (IWeavingService) weavingServiceTracker
                .getService();
        if (singletonWeavingService != null) {
            final BaseData baseData = loader.getClasspathManager()
                    .getBaseData();
            final State state = baseData.getAdaptor().getState();
            final Bundle bundle = baseData.getBundle();
            final BundleDescription bundleDescription = state.getBundle(bundle
                    .getBundleId());
            weavingService = singletonWeavingService.getInstance(
                    (ClassLoader) loader, bundle, state, bundleDescription,
                    supplementerRegistry);
        }
        if (Debug.DEBUG_WEAVE)
            Debug
                    .println("< AspectJAdaptorFactory.getWeavingService() service="
                            + weavingService);
        return weavingService;
    }

    private void initializePackageAdminService(final BundleContext context) {
        if (Debug.DEBUG_GENERAL)
            Debug
                    .println("> AspectJAdaptorFactory.initializePackageAdminService() context="
                            + context);

        final ServiceReference ref = context
                .getServiceReference(PackageAdmin.class.getName());
        if (ref != null) {
            packageAdminService = (PackageAdmin) context.getService(ref);
        }

        if (Debug.DEBUG_GENERAL)
            Debug
                    .println("< AspectJAdaptorFactory.initializePackageAdminService() "
                            + packageAdminService);
    }
}

Back to the top