KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > components > manager > AbstractSystemManager


1 /* ====================================================================
2  * Loom Software License, version 1.1
3  *
4  * Copyright (c) 2003, Loom Group. All rights reserved.
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. 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  *
17  * 3. Neither the name of the Loom Group nor the name "Loom" nor
18  * the names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior
20  * written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * ====================================================================
36  *
37  * Loom includes code from the Apache Software Foundation
38  *
39  * ====================================================================
40  * The Apache Software License, Version 1.1
41  *
42  * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
43  * reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  *
49  * 1. Redistributions of source code must retain the above copyright
50  * notice, this list of conditions and the following disclaimer.
51  *
52  * 2. Redistributions in binary form must reproduce the above copyright
53  * notice, this list of conditions and the following disclaimer in
54  * the documentation and/or other materials provided with the
55  * distribution.
56  *
57  * 3. The end-user documentation included with the redistribution,
58  * if any, must include the following acknowledgment:
59  * "This product includes software developed by the
60  * Apache Software Foundation (http://www.apache.org/)."
61  * Alternately, this acknowledgment may appear in the software
62  * itself, if and wherever such third-party acknowledgments
63  * normally appear.
64  *
65  * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
66  * must not be used to endorse or promote products derived from this
67  * software without prior written permission. For written
68  * permission, please contact apache@apache.org.
69  *
70  * 5. Products derived from this software may not be called "Apache",
71  * nor may "Apache" appear in their name, without prior written
72  * permission of the Apache Software Foundation.
73  *
74  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
75  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
76  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
77  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
78  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
79  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
80  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
81  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
82  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
83  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
84  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85  * SUCH DAMAGE.
86  */

87 package org.codehaus.loom.components.manager;
88
89 import java.util.HashMap JavaDoc;
90 import java.util.Map JavaDoc;
91 import org.codehaus.dna.AbstractLogEnabled;
92 import org.codehaus.dna.Active;
93 import org.codehaus.loom.interfaces.LoomException;
94 import org.codehaus.loom.interfaces.SystemManager;
95
96 /**
97  * This is abstract implementation of SystemManager.
98  *
99  * @author Peter Donald
100  */

101 public abstract class AbstractSystemManager
102     extends AbstractLogEnabled
103     implements SystemManager, Active
104 {
105     private final Map JavaDoc m_entries = new HashMap JavaDoc();
106
107     private SubContext m_subContext;
108
109     public void initialize()
110         throws Exception JavaDoc
111     {
112         m_subContext = new SubContext( this, null, null );
113     }
114
115     public void dispose()
116     {
117         m_subContext = null;
118     }
119
120     /**
121      * @see SystemManager#register(String, Object)
122      */

123     public synchronized void register( final String JavaDoc name,
124                                        final Object JavaDoc object )
125         throws LoomException, IllegalArgumentException JavaDoc
126     {
127         checkRegister( name, object );
128
129         final Object JavaDoc exportedObject = export( name, object );
130         m_entries.put( name, exportedObject );
131     }
132
133     /**
134      * @see SystemManager#unregister(String)
135      */

136     public synchronized void unregister( final String JavaDoc name )
137         throws LoomException
138     {
139         final Object JavaDoc entry = m_entries.remove( name );
140         if( null == entry )
141         {
142             return;
143         }
144
145         unexport( name, entry );
146     }
147
148     /**
149      * Returns the subcontext of the specified name. If it does not exist it is
150      * created.
151      *
152      * @return the subcontext with the specified name
153      * @throws LoomException if context cannot be created or retrieved
154      */

155     public SystemManager getSubContext( final String JavaDoc parent,
156                                         final String JavaDoc type )
157         throws LoomException
158     {
159         return m_subContext.getSubContext( parent, type );
160     }
161
162     /**
163      * Export the object to the particular management medium using the supplied
164      * object and interfaces. This needs to be implemented by subclasses.
165      *
166      * @param name the name of object
167      * @param object the object
168      * @return the exported object
169      * @throws LoomException if an error occurs
170      */

171     protected abstract Object JavaDoc export( String JavaDoc name, Object JavaDoc object )
172         throws LoomException;
173
174     /**
175      * Stop the exported object from being managed.
176      *
177      * @param name the name of object
178      * @param exportedObject the object return by export
179      * @throws LoomException if an error occurs
180      */

181     protected abstract void unexport( String JavaDoc name, Object JavaDoc exportedObject )
182         throws LoomException;
183
184     /**
185      * Helper method to help check before an objects registration. Verifies name
186      * and object are not null and verifies no entry exists using name.
187      *
188      * @param name the name of object
189      * @param object the object to be registered
190      * @throws LoomException if name already exists
191      * @throws IllegalArgumentException if name or object is null
192      */

193     private void checkRegister( final String JavaDoc name, final Object JavaDoc object )
194         throws LoomException, IllegalArgumentException JavaDoc
195     {
196         if( null == object )
197         {
198             throw new NullPointerException JavaDoc( "object" );
199         }
200         if( null == name )
201         {
202             throw new NullPointerException JavaDoc( "name" );
203         }
204
205         if( null != m_entries.get( name ) )
206         {
207             final String JavaDoc message = name +
208                 " already registered in SystemManager";
209             throw new LoomException( message );
210         }
211     }
212 }
213
Popular Tags