KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ungoverned > oscar > StartLevelImpl


1 /*
2  * Oscar - An implementation of the OSGi framework.
3  * Copyright (c) 2004, Richard S. Hall
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of the ungoverned.org nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Contact: Richard S. Hall (heavy@ungoverned.org)
33  * Contributor(s):
34  *
35 **/

36 package org.ungoverned.oscar;
37
38 import java.security.AccessController JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.List JavaDoc;
41
42 import org.osgi.framework.AdminPermission;
43 import org.osgi.framework.Bundle;
44 import org.osgi.service.startlevel.StartLevel;
45
46 /**
47  * @author rickhall
48  *
49  * To change the template for this generated type comment go to
50  * Window>Preferences>Java>Code Generation>Code and Comments
51 **/

52 public class StartLevelImpl implements StartLevel, Runnable JavaDoc
53 {
54     private Oscar m_oscar = null;
55     private List JavaDoc m_requestList = null;
56     // Reusable admin permission.
57
private static AdminPermission m_adminPerm = new AdminPermission();
58     
59     public StartLevelImpl(Oscar oscar)
60     {
61         m_oscar = oscar;
62         m_requestList = new ArrayList JavaDoc();
63
64         // Start a thread to perform asynchronous package refreshes.
65
Thread JavaDoc t = new Thread JavaDoc(this, "OscarStartLevel");
66         t.setDaemon(true);
67         t.start();
68     }
69     
70     /* (non-Javadoc)
71      * @see org.osgi.service.startlevel.StartLevel#getStartLevel()
72     **/

73     public int getStartLevel()
74     {
75         return m_oscar.getStartLevel();
76     }
77
78     /* (non-Javadoc)
79      * @see org.osgi.service.startlevel.StartLevel#setStartLevel(int)
80     **/

81     public void setStartLevel(int startlevel)
82     {
83         if (System.getSecurityManager() != null)
84         {
85             AccessController.checkPermission(m_adminPerm);
86         }
87         else if (startlevel <= 0)
88         {
89             throw new IllegalArgumentException JavaDoc(
90                 "Start level must be greater than zero.");
91         }
92         synchronized (m_requestList)
93         {
94             m_requestList.add(new Integer JavaDoc(startlevel));
95             m_requestList.notifyAll();
96         }
97     }
98
99     /* (non-Javadoc)
100      * @see org.osgi.service.startlevel.StartLevel#getBundleStartLevel(org.osgi.framework.Bundle)
101     **/

102     public int getBundleStartLevel(Bundle bundle)
103     {
104         return m_oscar.getBundleStartLevel(bundle);
105     }
106
107     /* (non-Javadoc)
108      * @see org.osgi.service.startlevel.StartLevel#setBundleStartLevel(org.osgi.framework.Bundle, int)
109     **/

110     public void setBundleStartLevel(Bundle bundle, int startlevel)
111     {
112         m_oscar.setBundleStartLevel(bundle, startlevel);
113     }
114
115     /* (non-Javadoc)
116      * @see org.osgi.service.startlevel.StartLevel#getInitialBundleStartLevel()
117     **/

118     public int getInitialBundleStartLevel()
119     {
120         return m_oscar.getInitialBundleStartLevel();
121     }
122
123     /* (non-Javadoc)
124      * @see org.osgi.service.startlevel.StartLevel#setInitialBundleStartLevel(int)
125     **/

126     public void setInitialBundleStartLevel(int startlevel)
127     {
128         m_oscar.setInitialBundleStartLevel(startlevel);
129     }
130
131     /* (non-Javadoc)
132      * @see org.osgi.service.startlevel.StartLevel#isBundlePersistentlyStarted(org.osgi.framework.Bundle)
133     **/

134     public boolean isBundlePersistentlyStarted(Bundle bundle)
135     {
136         return m_oscar.isBundlePersistentlyStarted(bundle);
137     }
138
139     public void run()
140     {
141         int startLevel = 0;
142
143         // This thread loops forever, thus it should
144
// be a daemon thread.
145
while (true)
146         {
147             synchronized (m_requestList)
148             {
149                 // Wait for a request.
150
while (m_requestList.size() == 0)
151                 {
152                     try {
153                         m_requestList.wait();
154                     } catch (InterruptedException JavaDoc ex) {
155                     }
156                 }
157                 
158                 // Get the requested start level.
159
startLevel = ((Integer JavaDoc) m_requestList.remove(0)).intValue();
160             }
161
162             // Set the new start level.
163
m_oscar.setStartLevelInternal(startLevel);
164         }
165     }
166 }
Popular Tags