KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > internal > protocol > MultiplexingFactory


1 /*******************************************************************************
2  * Copyright (c) 2006 Cognos Incorporated, IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  *******************************************************************************/

9 package org.eclipse.osgi.framework.internal.protocol;
10
11 import java.lang.reflect.Method JavaDoc;
12 import java.util.*;
13 import org.eclipse.osgi.framework.adaptor.FrameworkAdaptor;
14 import org.eclipse.osgi.framework.log.FrameworkLogEntry;
15 import org.osgi.framework.BundleContext;
16 import org.osgi.service.packageadmin.PackageAdmin;
17 import org.osgi.util.tracker.ServiceTracker;
18
19 /*
20  * An abstract class for handler factory impls (Stream and Content) that can
21  * handle environments running multiple osgi frameworks with the same VM.
22  */

23 public abstract class MultiplexingFactory {
24
25     protected static final String JavaDoc PACKAGEADMINCLASS = "org.osgi.service.packageadmin.PackageAdmin"; //$NON-NLS-1$
26
protected BundleContext context;
27     protected FrameworkAdaptor adaptor;
28     private List factories; // list of multiplexed factories
29
private ServiceTracker packageAdminTracker;
30
31     // used to get access to the protected SecurityManager#getClassContext method
32
private static class InternalSecurityManager extends SecurityManager JavaDoc {
33         public Class JavaDoc[] getClassContext() {
34             return super.getClassContext();
35         }
36     }
37
38     private static InternalSecurityManager internalSecurityManager = new InternalSecurityManager();
39
40     MultiplexingFactory(BundleContext context, FrameworkAdaptor adaptor) {
41         this.context = context;
42         this.adaptor = adaptor;
43         packageAdminTracker = new ServiceTracker(context, PACKAGEADMINCLASS, null);
44         packageAdminTracker.open();
45     }
46
47     abstract public void setParentFactory(Object JavaDoc parentFactory);
48
49     abstract public Object JavaDoc getParentFactory();
50
51     public synchronized boolean isMultiplexing() {
52         return factories != null;
53     }
54
55     public synchronized void register(Object JavaDoc factory) {
56         if (factories == null)
57             factories = new LinkedList();
58
59         // set parent for each factory so they can do proper delegation
60
try {
61             Class JavaDoc clazz = factory.getClass();
62             Method JavaDoc setParentFactory = clazz.getMethod("setParentFactory", new Class JavaDoc[] {Object JavaDoc.class}); //$NON-NLS-1$
63
setParentFactory.invoke(factory, new Object JavaDoc[] {getParentFactory()});
64         } catch (Exception JavaDoc e) {
65             adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingFactory.class.getName(), FrameworkLogEntry.ERROR, 0, "register", FrameworkLogEntry.ERROR, e, null)); //$NON-NLS-1$
66
throw new RuntimeException JavaDoc(e.getMessage());
67         }
68         factories.add(factory);
69     }
70
71     public synchronized void unregister(Object JavaDoc factory) {
72         factories.remove(factory);
73         if (factories.isEmpty())
74             factories = null;
75         // close the service tracker
76
try {
77             // this is brittle; if class does not directly extend MultplexingFactory then this method will not exist, but we do not want a public method here
78
Method JavaDoc closeTracker = factory.getClass().getSuperclass().getDeclaredMethod("closePackageAdminTracker", null); //$NON-NLS-1$
79
closeTracker.setAccessible(true); // its a private method
80
closeTracker.invoke(factory, null);
81         } catch (Exception JavaDoc e) {
82             adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingFactory.class.getName(), FrameworkLogEntry.ERROR, 0, "unregister", FrameworkLogEntry.ERROR, e, null)); //$NON-NLS-1$
83
throw new RuntimeException JavaDoc(e.getMessage());
84         }
85     }
86
87     public synchronized Object JavaDoc designateSuccessor() {
88         Object JavaDoc parentFactory = getParentFactory();
89         if (factories == null || factories.isEmpty())
90             return parentFactory;
91
92         Object JavaDoc successor = factories.remove(0);
93         try {
94             Class JavaDoc clazz = successor.getClass();
95             Method JavaDoc register = clazz.getMethod("register", new Class JavaDoc[] {Object JavaDoc.class}); //$NON-NLS-1$
96
for (Iterator it = factories.iterator(); it.hasNext();) {
97                 register.invoke(successor, new Object JavaDoc[] {it.next()});
98             }
99         } catch (Exception JavaDoc e) {
100             adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingFactory.class.getName(), FrameworkLogEntry.ERROR, 0, "designateSuccessor", FrameworkLogEntry.ERROR, e, null)); //$NON-NLS-1$
101
throw new RuntimeException JavaDoc(e.getMessage());
102         }
103         factories = null;
104         closePackageAdminTracker(); // close tracker
105
return successor;
106     }
107
108     private void closePackageAdminTracker() {
109         packageAdminTracker.close();
110     }
111
112     public synchronized Object JavaDoc findAuthorizedFactory(List ignoredClasses) {
113         Class JavaDoc[] classStack = internalSecurityManager.getClassContext();
114         for (int i = 0; i < classStack.length; i++) {
115             Class JavaDoc clazz = classStack[i];
116             if (clazz == InternalSecurityManager.class || clazz == MultiplexingFactory.class || ignoredClasses.contains(clazz))
117                 continue;
118             if (hasAuthority(clazz))
119                 return this;
120             if (factories == null)
121                 continue;
122             for (Iterator it = factories.iterator(); it.hasNext();) {
123                 Object JavaDoc factory = it.next();
124                 try {
125                     Method JavaDoc hasAuthorityMethod = factory.getClass().getMethod("hasAuthority", new Class JavaDoc[] {Class JavaDoc.class}); //$NON-NLS-1$
126
if (((Boolean JavaDoc) hasAuthorityMethod.invoke(factory, new Object JavaDoc[] {clazz})).booleanValue()) {
127                         return factory;
128                     }
129                 } catch (Exception JavaDoc e) {
130                     adaptor.getFrameworkLog().log(new FrameworkLogEntry(MultiplexingFactory.class.getName(), FrameworkLogEntry.ERROR, 0, "findAuthorizedURLStreamHandler-loop", FrameworkLogEntry.ERROR, e, null)); //$NON-NLS-1$
131
throw new RuntimeException JavaDoc(e.getMessage());
132                 }
133             }
134         }
135         return null;
136     }
137
138     public boolean hasAuthority(Class JavaDoc clazz) {
139         PackageAdmin packageAdminService = (PackageAdmin) packageAdminTracker.getService();
140         if (packageAdminService != null) {
141             return packageAdminService.getBundle(clazz) != null;
142         }
143         return false;
144     }
145 }
146
Popular Tags