blob: 6f2d84ff823029f51de8cc74ce250c4b4b68d497 [file] [log] [blame]
Richard S. Hall864dc662006-09-26 16:53:29 +00001/*
Richard S. Hallb3951672006-09-19 17:04:53 +00002 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
Richard S. Hall29a4fbc2006-02-03 12:54:52 +00009 *
Richard S. Hallb3951672006-09-19 17:04:53 +000010 * http://www.apache.org/licenses/LICENSE-2.0
Richard S. Hall29a4fbc2006-02-03 12:54:52 +000011 *
Richard S. Hallb3951672006-09-19 17:04:53 +000012 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
Richard S. Hall29a4fbc2006-02-03 12:54:52 +000018 */
19package org.apache.felix.moduleloader;
20
21import java.net.URL;
Richard S. Hall864dc662006-09-26 16:53:29 +000022import java.util.Enumeration;
Richard S. Hall29a4fbc2006-02-03 12:54:52 +000023
24import org.apache.felix.framework.Logger;
25
26public class ModuleImpl implements IModule
27{
28 private Logger m_logger = null;
29 private String m_id = null;
Richard S. Hall594145f2006-07-23 13:07:18 +000030 private boolean m_removalPending = false;
31 private IModuleDefinition m_md = null;
Richard S. Hall29a4fbc2006-02-03 12:54:52 +000032 private IContentLoader m_contentLoader = null;
Richard S. Hall594145f2006-07-23 13:07:18 +000033 private IWire[] m_wires = null;
Richard S. Hall771843f2007-06-14 18:13:03 +000034 private IModule[] m_dependents = new IModule[0];
Karl Paulsc4ac2b02006-08-24 12:28:41 +000035 private Object m_securityContext = null;
Richard S. Hall29a4fbc2006-02-03 12:54:52 +000036
Richard S. Hall594145f2006-07-23 13:07:18 +000037 ModuleImpl(Logger logger, String id, IModuleDefinition md)
Richard S. Hall29a4fbc2006-02-03 12:54:52 +000038 {
39 m_logger = logger;
40 m_id = id;
Richard S. Hall594145f2006-07-23 13:07:18 +000041 m_md = md;
Richard S. Hall29a4fbc2006-02-03 12:54:52 +000042 }
43
44 public String getId()
45 {
46 return m_id;
47 }
48
Richard S. Hall594145f2006-07-23 13:07:18 +000049 public IModuleDefinition getDefinition()
50 {
51 return m_md;
52 }
53
Richard S. Hall29a4fbc2006-02-03 12:54:52 +000054 public IContentLoader getContentLoader()
55 {
56 return m_contentLoader;
57 }
58
59 protected void setContentLoader(IContentLoader contentLoader)
60 {
61 m_contentLoader = contentLoader;
62 }
63
Karl Paulsc4ac2b02006-08-24 12:28:41 +000064 protected void setSecurityContext(Object securityContext)
65 {
66 m_securityContext = securityContext;
67 }
68
Richard S. Hall771843f2007-06-14 18:13:03 +000069 public synchronized IWire[] getWires()
Richard S. Hall594145f2006-07-23 13:07:18 +000070 {
71 return m_wires;
72 }
73
Richard S. Hall771843f2007-06-14 18:13:03 +000074 public synchronized void setWires(IWire[] wires)
Richard S. Hall594145f2006-07-23 13:07:18 +000075 {
Richard S. Hall771843f2007-06-14 18:13:03 +000076 // Remove module from old wire modules' dependencies.
77 for (int i = 0; (m_wires != null) && (i < m_wires.length); i++)
78 {
79 ((ModuleImpl) m_wires[i].getExporter()).removeDependent(this);
80 }
Richard S. Hall594145f2006-07-23 13:07:18 +000081 m_wires = wires;
Richard S. Hall771843f2007-06-14 18:13:03 +000082 // Add module to new wire modules' dependencies.
83 for (int i = 0; (wires != null) && (i < wires.length); i++)
84 {
85 ((ModuleImpl) m_wires[i].getExporter()).addDependent(this);
86 }
87 }
88
89 public synchronized void addDependent(IModule module)
90 {
91 // Make sure the dependent module is not already present.
92 for (int i = 0; i < m_dependents.length; i++)
93 {
94 if (m_dependents[i].equals(module))
95 {
96 return;
97 }
98 }
99 IModule[] tmp = new IModule[m_dependents.length + 1];
100 System.arraycopy(m_dependents, 0, tmp, 0, m_dependents.length);
101 tmp[m_dependents.length] = module;
102 m_dependents = tmp;
103 }
104
105 public synchronized void removeDependent(IModule module)
106 {
107 // Make sure the dependent module is not already present.
108 for (int i = 0; i < m_dependents.length; i++)
109 {
110 if (m_dependents[i].equals(module))
111 {
112 // If this is the module, then point to empty list.
113 if ((m_dependents.length - 1) == 0)
114 {
115 m_dependents = new IModule[0];
116 }
117 // Otherwise, we need to do some array copying.
118 else
119 {
120 IModule[] tmp = new IModule[m_dependents.length - 1];
121 System.arraycopy(m_dependents, 0, tmp, 0, i);
122 if (i < tmp.length)
123 {
124 System.arraycopy(
125 m_dependents, i + 1, tmp, i, tmp.length - i);
126 }
127 m_dependents = tmp;
128 }
129 }
130 }
131 }
132
133 public synchronized IModule[] getDependents()
134 {
135 return m_dependents;
Richard S. Hall594145f2006-07-23 13:07:18 +0000136 }
137
138 public boolean isRemovalPending()
139 {
140 return m_removalPending;
141 }
142
143 public void setRemovalPending(boolean removalPending)
144 {
145 m_removalPending = removalPending;
146 }
147
Richard S. Hall29a4fbc2006-02-03 12:54:52 +0000148 public Class getClass(String name)
149 {
150 try
151 {
152 return m_contentLoader.getSearchPolicy().findClass(name);
153 }
154 catch (ClassNotFoundException ex)
155 {
156 m_logger.log(
157 Logger.LOG_WARNING,
158 ex.getMessage(),
159 ex);
160 }
161 return null;
162 }
163
164 public URL getResource(String name)
165 {
166 try
167 {
168 return m_contentLoader.getSearchPolicy().findResource(name);
169 }
170 catch (ResourceNotFoundException ex)
171 {
172 m_logger.log(
173 Logger.LOG_WARNING,
174 ex.getMessage(),
175 ex);
176 }
177 return null;
178 }
179
Richard S. Hall864dc662006-09-26 16:53:29 +0000180 public Enumeration getResources(String name)
181 {
182 try
183 {
184 return m_contentLoader.getSearchPolicy().findResources(name);
185 }
186 catch (ResourceNotFoundException ex)
187 {
188 m_logger.log(
189 Logger.LOG_WARNING,
190 ex.getMessage(),
191 ex);
192 }
193 return null;
194 }
195
Richard S. Hall29a4fbc2006-02-03 12:54:52 +0000196 public String toString()
197 {
198 return m_id;
199 }
Karl Paulsc4ac2b02006-08-24 12:28:41 +0000200
201 public Object getSecurityContext()
202 {
203 return m_securityContext;
204 }
Richard S. Hall29a4fbc2006-02-03 12:54:52 +0000205}