blob: 646db1d459c8d4be0875010b38d78de84487994b [file] [log] [blame]
Richard S. Hall7fa14152006-06-14 15:22:03 +00001/*
2 * Copyright 2006 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 */
17package org.apache.felix.ipojo;
18
19import java.util.logging.Level;
20
21import org.apache.felix.ipojo.metadata.Element;
22import org.osgi.framework.BundleContext;
23
24/**
25 * The component manager factory class manages component manager object.
Richard S. Hall749e4592006-06-22 12:51:44 +000026 * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
Richard S. Hall7fa14152006-06-14 15:22:03 +000027 */
28public class ComponentManagerFactory {
29
30 // Fields :
31 /**
32 * List of the managed component manager.
33 */
34 private ComponentManager[] m_componentManagers = new ComponentManager[0];
35
36 /**
37 * The bundle context reference.
38 */
39 private BundleContext m_bundleContext = null;
40
41 //End field
42
43 // Field accessors
44
45 /**
46 * Add a component manager factory to the component manager list.
47 * @param cm : the new component metadata.
48 */
49 private void addComponent(ComponentManager cm) {
50
51 // If the component manager array is not empty add the new factory at the end
52 if (m_componentManagers.length != 0) {
53 ComponentManager[] newCM = new ComponentManager[m_componentManagers.length + 1];
54 System.arraycopy(m_componentManagers, 0, newCM, 0, m_componentManagers.length);
55 newCM[m_componentManagers.length] = cm;
56 m_componentManagers = newCM;
57 }
58 // Else create an array of size one with the new component manager
59 else {
60 m_componentManagers = new ComponentManager[] {cm};
61 }
62 }
63
64 /**
65 * Remove the component manager for m the list.
66 * @param cm : the component manager to remove
67 */
68 public void removeComponent(ComponentManager cm) {
69 cm.stop();
70 int idx = -1;
71
72 for (int i = 0; i < m_componentManagers.length; i++) {
73 if (m_componentManagers[i] == cm) { idx = i; }
74 }
75
76 if (idx >= 0) {
77 if ((m_componentManagers.length - 1) == 0) { m_componentManagers = new ComponentManager[0]; }
78 else {
79 ComponentManager[] newCMList = new ComponentManager[m_componentManagers.length - 1];
80 System.arraycopy(m_componentManagers, 0, newCMList, 0, idx);
81 if (idx < newCMList.length) {
82 System.arraycopy(m_componentManagers, idx + 1, newCMList, idx, newCMList.length - idx); }
83 m_componentManagers = newCMList;
84 }
85 }
86 }
87
88 /**
89 * @return the iPOJO activator reference
90 */
91 public BundleContext getBundleContext() { return m_bundleContext; }
92
93 // End field accessors
94
95 /**
96 * Constructor of a ComponentManagerFactory from a component metadata.
97 * This contructor is use when the iPOJO Activator is used.
98 * @param cm : Component Metadata for the component factory
99 */
100 protected ComponentManagerFactory(Activator activator, Element cm) {
101 m_bundleContext = activator.getBundleContext();
102 createComponentManager(cm);
103 }
104
105 /**
106 * Create a component manager factory and create a component manager with the given medatada.
107 * @param bc : bundle context
108 * @param cm : metadata of the component to create
109 */
110 public ComponentManagerFactory(BundleContext bc, Element cm) {
111 m_bundleContext = bc;
112 createComponentManager(cm);
113 }
114
115 /**
116 * Create a component manager factory, no component manager are created.
117 * @param bc
118 */
119 public ComponentManagerFactory(BundleContext bc) {
120 m_bundleContext = bc;
121 }
122
123 /**
124 * Create a component manager form the component metadata.
125 * @param cm : Component Metadata
126 * @return a component manager configured with the metadata
127 */
128 public ComponentManager createComponentManager(Element cm) {
129 ComponentManager component = new ComponentManager(this);
130 component.configure(cm);
131 addComponent(component);
132 return component;
133 }
134
135 // Factory lifecycle management
136
137 /**
138 * Stop all the component managers.
139 */
140 public void stop() {
141 Activator.getLogger().log(Level.INFO, "[Bundle " + m_bundleContext.getBundle().getBundleId() + "] Stop the component factory");
142 for (int i = 0; i < m_componentManagers.length; i++) {
143 ComponentManager cm = m_componentManagers[i];
144 cm.stop();
145 }
146 }
147
148 /**
149 * Start all the component managers.
150 */
151 public void start() {
152 Activator.getLogger().log(Level.INFO, "[Bundle " + m_bundleContext.getBundle().getBundleId() + "] Start the component factory");
153 for (int i = 0; i < m_componentManagers.length; i++) {
154 ComponentManager cm = m_componentManagers[i];
155 cm.start();
156 }
157 }
158
159}