blob: 800813311e4358dcaacbc7f0cac284909901933d [file] [log] [blame]
Richard S. Hall930fecc2005-08-16 18:33:34 +00001/*
2 * Copyright 2005 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
Richard S. Hall5a031592005-08-19 19:53:58 +000017package org.apache.felix.framework;
Richard S. Hall930fecc2005-08-16 18:33:34 +000018
19import java.security.AccessController;
20import java.util.ArrayList;
21import java.util.List;
22
23import org.osgi.framework.AdminPermission;
24import org.osgi.framework.Bundle;
25import org.osgi.service.startlevel.StartLevel;
26
27/**
28 * @author rickhall
29 *
30 * To change the template for this generated type comment go to
31 * Window>Preferences>Java>Code Generation>Code and Comments
32**/
33public class StartLevelImpl implements StartLevel, Runnable
34{
35 private Felix m_felix = null;
36 private List m_requestList = null;
37 // Reusable admin permission.
38 private static AdminPermission m_adminPerm = new AdminPermission();
39
40 public StartLevelImpl(Felix felix)
41 {
42 m_felix = felix;
43 m_requestList = new ArrayList();
44
45 // Start a thread to perform asynchronous package refreshes.
46 Thread t = new Thread(this, "FelixStartLevel");
47 t.setDaemon(true);
48 t.start();
49 }
50
51 /* (non-Javadoc)
52 * @see org.osgi.service.startlevel.StartLevel#getStartLevel()
53 **/
54 public int getStartLevel()
55 {
56 return m_felix.getStartLevel();
57 }
58
59 /* (non-Javadoc)
60 * @see org.osgi.service.startlevel.StartLevel#setStartLevel(int)
61 **/
62 public void setStartLevel(int startlevel)
63 {
64 if (System.getSecurityManager() != null)
65 {
66 AccessController.checkPermission(m_adminPerm);
67 }
68 else if (startlevel <= 0)
69 {
70 throw new IllegalArgumentException(
71 "Start level must be greater than zero.");
72 }
73 synchronized (m_requestList)
74 {
75 m_requestList.add(new Integer(startlevel));
76 m_requestList.notifyAll();
77 }
78 }
79
80 /* (non-Javadoc)
81 * @see org.osgi.service.startlevel.StartLevel#getBundleStartLevel(org.osgi.framework.Bundle)
82 **/
83 public int getBundleStartLevel(Bundle bundle)
84 {
85 return m_felix.getBundleStartLevel(bundle);
86 }
87
88 /* (non-Javadoc)
89 * @see org.osgi.service.startlevel.StartLevel#setBundleStartLevel(org.osgi.framework.Bundle, int)
90 **/
91 public void setBundleStartLevel(Bundle bundle, int startlevel)
92 {
93 m_felix.setBundleStartLevel(bundle, startlevel);
94 }
95
96 /* (non-Javadoc)
97 * @see org.osgi.service.startlevel.StartLevel#getInitialBundleStartLevel()
98 **/
99 public int getInitialBundleStartLevel()
100 {
101 return m_felix.getInitialBundleStartLevel();
102 }
103
104 /* (non-Javadoc)
105 * @see org.osgi.service.startlevel.StartLevel#setInitialBundleStartLevel(int)
106 **/
107 public void setInitialBundleStartLevel(int startlevel)
108 {
109 m_felix.setInitialBundleStartLevel(startlevel);
110 }
111
112 /* (non-Javadoc)
113 * @see org.osgi.service.startlevel.StartLevel#isBundlePersistentlyStarted(org.osgi.framework.Bundle)
114 **/
115 public boolean isBundlePersistentlyStarted(Bundle bundle)
116 {
117 return m_felix.isBundlePersistentlyStarted(bundle);
118 }
119
120 public void run()
121 {
122 int startLevel = 0;
123
124 // This thread loops forever, thus it should
125 // be a daemon thread.
126 while (true)
127 {
128 synchronized (m_requestList)
129 {
130 // Wait for a request.
131 while (m_requestList.size() == 0)
132 {
133 try {
134 m_requestList.wait();
135 } catch (InterruptedException ex) {
136 }
137 }
138
139 // Get the requested start level.
140 startLevel = ((Integer) m_requestList.remove(0)).intValue();
141 }
142
143 // Set the new start level.
144 m_felix.setFrameworkStartLevel(startLevel);
145 }
146 }
147}