KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > util > FrameworkMessageFormat


1 /*******************************************************************************
2  * Copyright (c) 2003, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.osgi.framework.util;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.*;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Locale JavaDoc;
19 import org.eclipse.osgi.framework.msg.MessageFormat;
20 import org.eclipse.osgi.service.systembundle.EntryLocator;
21 import org.osgi.framework.*;
22
23 public class FrameworkMessageFormat extends MessageFormat {
24     static private BundleContext context;
25     static private ArrayList JavaDoc msgList = new ArrayList JavaDoc();
26     static private ServiceListener listener = null;
27     static private EntryLocator systemEntryLocator = null;
28
29     private Class JavaDoc clazz;
30     private String JavaDoc bundleName;
31
32     private FrameworkMessageFormat(String JavaDoc bundleName, Locale JavaDoc locale, Class JavaDoc clazz) {
33         super(bundleName, locale, clazz);
34         this.bundleName = bundleName;
35         this.clazz = clazz;
36     }
37
38     protected void init(final String JavaDoc bundleName, final Locale JavaDoc locale, final Class JavaDoc clazz) {
39         if (systemEntryLocator == null) {
40             super.init(bundleName, locale, clazz);
41             return;
42         }
43
44         URL JavaDoc resourceURL = systemEntryLocator.getProperties(bundleName, locale);
45         if (resourceURL != null) {
46             InputStream JavaDoc resourceStream = null;
47             try {
48                 resourceStream = resourceURL.openStream();
49                 bundle = new PropertyResourceBundle(resourceStream);
50                 this.locale = locale;
51                 return;
52             } catch (IOException JavaDoc e) {
53                 // Do nothing will just call super below
54
} finally {
55                 if (resourceStream != null) {
56                     try {
57                         resourceStream.close();
58                     } catch (IOException JavaDoc e) {
59                         //Ignore exception
60
}
61                 }
62             }
63         }
64         // if we get here just call super
65
super.init(bundleName, locale, clazz);
66     }
67
68     private void init() {
69         init(bundleName, locale, clazz);
70     }
71
72     static public synchronized void setContext(BundleContext context) {
73         if (context == null) {
74             if (FrameworkMessageFormat.context != null && listener != null)
75                 FrameworkMessageFormat.context.removeServiceListener(listener);
76             FrameworkMessageFormat.context = null;
77         } else if (FrameworkMessageFormat.context == null) {
78             FrameworkMessageFormat.context = context;
79             listener = new ResourceFinderListener();
80             try {
81                 FrameworkMessageFormat.context.addServiceListener(listener, "(objectclass=org.eclipse.osgi.service.systembundle.EntryLocator)"); //$NON-NLS-1$
82
} catch (InvalidSyntaxException e) {
83                 // Do nothing this cannot happen.
84
}
85         }
86
87     }
88
89     static public synchronized MessageFormat getMessageFormat(String JavaDoc bundleName) {
90         FrameworkMessageFormat msgFormat = new FrameworkMessageFormat(bundleName, Locale.getDefault(), FrameworkMessageFormat.class);
91         msgList.add(msgFormat);
92         return msgFormat;
93     }
94
95     static private synchronized void initMessages() {
96         int size = msgList.size();
97         for (int i = 0; i < size; i++) {
98             ((FrameworkMessageFormat) msgList.get(i)).init();
99         }
100     }
101
102     static private class ResourceFinderListener implements ServiceListener {
103         private ServiceReference resourceFinderRef = null;
104
105         public void serviceChanged(ServiceEvent event) {
106             synchronized (FrameworkMessageFormat.class) {
107                 int eventType = event.getType();
108                 ServiceReference sr = event.getServiceReference();
109                 switch (eventType) {
110                     case ServiceEvent.REGISTERED :
111                         if (systemEntryLocator == null) {
112                             resourceFinderRef = sr;
113                             systemEntryLocator = (EntryLocator) context.getService(sr);
114                             initMessages();
115                         }
116                         break;
117                     case ServiceEvent.UNREGISTERING :
118                         if (sr.equals(resourceFinderRef)) {
119                             systemEntryLocator = null;
120                             initMessages();
121                             context.ungetService(resourceFinderRef);
122                             resourceFinderRef = null;
123                         }
124                         break;
125                 }
126             }
127         }
128     }
129
130 }
Popular Tags