KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > tools > Service


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.fortress.tools;
19
20 import java.io.File JavaDoc;
21 import java.io.FileWriter JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27
28 /**
29  * Encapsulate the Service information, and encapsulate all the
30  * logic to serialize the service.
31  *
32  * @author <a HREF="mailto:dev@avalon.apache.org">The Avalon Team</a>
33  * @version CVS $Revision: 1.11 $ $Date: 2004/02/28 15:16:27 $
34  */

35 final class Service
36 {
37     private final Set JavaDoc m_components;
38
39     private final String JavaDoc m_type;
40
41     /**
42      * Initialize a service with the type name.
43      *
44      * @param type
45      */

46     public Service( final String JavaDoc type )
47     {
48         if ( type == null ) throw new NullPointerException JavaDoc( "type" );
49
50         m_type = type;
51         m_components = new HashSet JavaDoc();
52     }
53
54     /**
55      * Get the service type name.
56      *
57      * @return the type name
58      */

59     public String JavaDoc getType()
60     {
61         return m_type;
62     }
63
64     /**
65      * Add a component to the service.
66      *
67      * @param type the type name for the component
68      */

69     public void addComponent( final Component type )
70     {
71         if ( type == null ) throw new NullPointerException JavaDoc( "type" );
72
73         m_components.add( type );
74     }
75
76     public Iterator JavaDoc getComponents()
77     {
78         return m_components.iterator();
79     }
80
81     /**
82      * Output the service info.
83      *
84      * @param rootDir
85      * @throws IOException
86      */

87     public void serialize( final File JavaDoc rootDir ) throws IOException JavaDoc
88     {
89         if ( m_components.isEmpty() ) return;
90
91         final File JavaDoc serviceFile = new File JavaDoc( rootDir, "META-INF/services/" + getType() );
92         PrintWriter JavaDoc writer = null;
93
94         try
95         {
96             writer = new PrintWriter JavaDoc( new FileWriter JavaDoc( serviceFile ) );
97
98             final Iterator JavaDoc it = m_components.iterator();
99             while ( it.hasNext() )
100             {
101                 final Component comp = (Component) it.next();
102                 writer.println( comp.getType() );
103             }
104         }
105         finally
106         {
107             if ( null != writer )
108             {
109                 writer.close();
110             }
111         }
112     }
113 }
114
Popular Tags