Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a648e0ed2272b0c8d94f0e2a4a736a84d323c03b (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
/*******************************************************************************
 * Copyright (c) 2008, 2013 Heiko Seeberger and others.
 *
 * This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0.
 * 
 * Contributors:
 *   Heiko Seeberger           initial implementation
 *   Martin Lippert            extracted caching service factory
 *******************************************************************************/

package org.eclipse.equinox.weaving.internal.caching;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.eclipse.equinox.service.weaving.ICachingService;
import org.eclipse.equinox.service.weaving.ICachingServiceFactory;
import org.eclipse.osgi.service.debug.DebugOptions;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;

/**
 * {@link BundleActivator} for "org.aspectj.osgi.service.caching".
 * 
 * @author Heiko Seeberger
 */
public class Activator implements BundleActivator {

    private static boolean verbose = Boolean
            .getBoolean("org.aspectj.osgi.verbose"); //$NON-NLS-1$

    private CachingServiceFactory cachingServiceFactory;

    private ServiceRegistration<?> cachingServiceFactoryRegistration;

    /**
     * Registers a new {@link CachingServiceFactory} instance as OSGi service
     * under the interface {@link ICachingService}.
     * 
     * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
     */
    public void start(final BundleContext bundleContext) {
        setDebugEnabled(bundleContext);

        if (shouldRegister()) {
            if (verbose)
                System.err
                        .println("[org.eclipse.equinox.weaving.caching] info starting standard caching service ..."); //$NON-NLS-1$
            registerCachingServiceFactory(bundleContext);
        } else {
            if (verbose)
                System.err
                        .println("[org.eclipse.equinox.weaving.caching] warning cannot start standard caching service on J9 VM"); //$NON-NLS-1$
        }
    }

    /**
     * Shuts down the {@link CachingServiceFactory}.
     * 
     * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
     */
    public void stop(final BundleContext context) {
        if (cachingServiceFactoryRegistration != null) {
            cachingServiceFactory.stop();
            cachingServiceFactoryRegistration.unregister();
        }
        if (Log.isDebugEnabled()) {
            Log.debug("Shut down and unregistered SingletonCachingService."); //$NON-NLS-1$
        }
    }

    private void registerCachingServiceFactory(final BundleContext bundleContext) {
        cachingServiceFactory = new CachingServiceFactory(bundleContext);
        cachingServiceFactoryRegistration = bundleContext.registerService(
                ICachingServiceFactory.class.getName(), cachingServiceFactory,
                null);
        if (Log.isDebugEnabled()) {
            Log.debug("Created and registered SingletonCachingService."); //$NON-NLS-1$
        }
    }

    private void setDebugEnabled(final BundleContext bundleContext) {
        final ServiceReference<?> debugOptionsReference = bundleContext
                .getServiceReference(DebugOptions.class.getName());
        if (debugOptionsReference != null) {
            final DebugOptions debugOptions = (DebugOptions) bundleContext
                    .getService(debugOptionsReference);
            if (debugOptions != null) {
                Log.debugEnabled = debugOptions.getBooleanOption(
                        "org.eclipse.equinox.weaving.caching/debug", false); //$NON-NLS-1$
            }
        }
        if (debugOptionsReference != null) {
            bundleContext.ungetService(debugOptionsReference);
        }
    }

    private boolean shouldRegister() {
        boolean enabled = true;
        try {
            Class.forName("com.ibm.oti.vm.VM"); // if this fails we are not on J9 //$NON-NLS-1$
            final Class<?> sharedClass = Class
                    .forName("com.ibm.oti.shared.Shared"); //$NON-NLS-1$
            final Method isSharingEnabledMethod = sharedClass.getMethod(
                    "isSharingEnabled", (Class[]) null); //$NON-NLS-1$
            if (isSharingEnabledMethod != null) {
                final Boolean sharing = (Boolean) isSharingEnabledMethod
                        .invoke(null, (Object[]) null);
                if (sharing != null && sharing.booleanValue()) {
                    enabled = false;
                }
            }
        } catch (final ClassNotFoundException ex) {
        } catch (final SecurityException e) {
        } catch (final NoSuchMethodException e) {
        } catch (final IllegalArgumentException e) {
        } catch (final IllegalAccessException e) {
        } catch (final InvocationTargetException e) {
        }

        return enabled;
    }
}

Back to the top