Richard S. Hall | 864dc66 | 2006-09-26 16:53:29 +0000 | [diff] [blame] | 1 | /* |
Richard S. Hall | b395167 | 2006-09-19 17:04:53 +0000 | [diff] [blame] | 2 | * 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. Hall | 29a4fbc | 2006-02-03 12:54:52 +0000 | [diff] [blame] | 9 | * |
Richard S. Hall | b395167 | 2006-09-19 17:04:53 +0000 | [diff] [blame] | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
Richard S. Hall | 29a4fbc | 2006-02-03 12:54:52 +0000 | [diff] [blame] | 11 | * |
Richard S. Hall | b395167 | 2006-09-19 17:04:53 +0000 | [diff] [blame] | 12 | * 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. Hall | 29a4fbc | 2006-02-03 12:54:52 +0000 | [diff] [blame] | 18 | */ |
| 19 | package org.apache.felix.moduleloader; |
| 20 | |
| 21 | import java.net.URL; |
Richard S. Hall | 864dc66 | 2006-09-26 16:53:29 +0000 | [diff] [blame] | 22 | import java.util.Enumeration; |
Richard S. Hall | 29a4fbc | 2006-02-03 12:54:52 +0000 | [diff] [blame] | 23 | |
| 24 | import org.apache.felix.framework.Logger; |
| 25 | |
| 26 | public class ModuleImpl implements IModule |
| 27 | { |
| 28 | private Logger m_logger = null; |
| 29 | private String m_id = null; |
Richard S. Hall | 594145f | 2006-07-23 13:07:18 +0000 | [diff] [blame] | 30 | private boolean m_removalPending = false; |
| 31 | private IModuleDefinition m_md = null; |
Richard S. Hall | 29a4fbc | 2006-02-03 12:54:52 +0000 | [diff] [blame] | 32 | private IContentLoader m_contentLoader = null; |
Richard S. Hall | 594145f | 2006-07-23 13:07:18 +0000 | [diff] [blame] | 33 | private IWire[] m_wires = null; |
Richard S. Hall | 771843f | 2007-06-14 18:13:03 +0000 | [diff] [blame^] | 34 | private IModule[] m_dependents = new IModule[0]; |
Karl Pauls | c4ac2b0 | 2006-08-24 12:28:41 +0000 | [diff] [blame] | 35 | private Object m_securityContext = null; |
Richard S. Hall | 29a4fbc | 2006-02-03 12:54:52 +0000 | [diff] [blame] | 36 | |
Richard S. Hall | 594145f | 2006-07-23 13:07:18 +0000 | [diff] [blame] | 37 | ModuleImpl(Logger logger, String id, IModuleDefinition md) |
Richard S. Hall | 29a4fbc | 2006-02-03 12:54:52 +0000 | [diff] [blame] | 38 | { |
| 39 | m_logger = logger; |
| 40 | m_id = id; |
Richard S. Hall | 594145f | 2006-07-23 13:07:18 +0000 | [diff] [blame] | 41 | m_md = md; |
Richard S. Hall | 29a4fbc | 2006-02-03 12:54:52 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | public String getId() |
| 45 | { |
| 46 | return m_id; |
| 47 | } |
| 48 | |
Richard S. Hall | 594145f | 2006-07-23 13:07:18 +0000 | [diff] [blame] | 49 | public IModuleDefinition getDefinition() |
| 50 | { |
| 51 | return m_md; |
| 52 | } |
| 53 | |
Richard S. Hall | 29a4fbc | 2006-02-03 12:54:52 +0000 | [diff] [blame] | 54 | public IContentLoader getContentLoader() |
| 55 | { |
| 56 | return m_contentLoader; |
| 57 | } |
| 58 | |
| 59 | protected void setContentLoader(IContentLoader contentLoader) |
| 60 | { |
| 61 | m_contentLoader = contentLoader; |
| 62 | } |
| 63 | |
Karl Pauls | c4ac2b0 | 2006-08-24 12:28:41 +0000 | [diff] [blame] | 64 | protected void setSecurityContext(Object securityContext) |
| 65 | { |
| 66 | m_securityContext = securityContext; |
| 67 | } |
| 68 | |
Richard S. Hall | 771843f | 2007-06-14 18:13:03 +0000 | [diff] [blame^] | 69 | public synchronized IWire[] getWires() |
Richard S. Hall | 594145f | 2006-07-23 13:07:18 +0000 | [diff] [blame] | 70 | { |
| 71 | return m_wires; |
| 72 | } |
| 73 | |
Richard S. Hall | 771843f | 2007-06-14 18:13:03 +0000 | [diff] [blame^] | 74 | public synchronized void setWires(IWire[] wires) |
Richard S. Hall | 594145f | 2006-07-23 13:07:18 +0000 | [diff] [blame] | 75 | { |
Richard S. Hall | 771843f | 2007-06-14 18:13:03 +0000 | [diff] [blame^] | 76 | // 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. Hall | 594145f | 2006-07-23 13:07:18 +0000 | [diff] [blame] | 81 | m_wires = wires; |
Richard S. Hall | 771843f | 2007-06-14 18:13:03 +0000 | [diff] [blame^] | 82 | // 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. Hall | 594145f | 2006-07-23 13:07:18 +0000 | [diff] [blame] | 136 | } |
| 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. Hall | 29a4fbc | 2006-02-03 12:54:52 +0000 | [diff] [blame] | 148 | 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. Hall | 864dc66 | 2006-09-26 16:53:29 +0000 | [diff] [blame] | 180 | 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. Hall | 29a4fbc | 2006-02-03 12:54:52 +0000 | [diff] [blame] | 196 | public String toString() |
| 197 | { |
| 198 | return m_id; |
| 199 | } |
Karl Pauls | c4ac2b0 | 2006-08-24 12:28:41 +0000 | [diff] [blame] | 200 | |
| 201 | public Object getSecurityContext() |
| 202 | { |
| 203 | return m_securityContext; |
| 204 | } |
Richard S. Hall | 29a4fbc | 2006-02-03 12:54:52 +0000 | [diff] [blame] | 205 | } |