KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > runtime > generator > apt > ControlClientManifest


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

20
21 import com.sun.mirror.apt.Filer;
22
23 import java.io.*;
24 import java.util.*;
25
26 /**
27  * The controls client manifest (aka "client manifest") surfaces the set of
28  * control types used by a client, and make the assembly process more
29  * efficient. The control client annotation processor generates a client
30  * manifest documenting the set of used control types. This manifest is a
31  * java.util.Properties file that specifies:
32  *
33  * - classname of the control client
34  * - classnames of each control type used by that control client (the set
35  * identified by @Control and @ControlReference usages) and the
36  * corresponding default implementation binding
37  *
38  * Example client manifest:
39  *
40  * FooImpl.controls.properties
41  * ---------------------------
42  * .client.name=org.acme.controls.FooImpl
43  * org.acme.controls.CustomerDbBean=org.apache.beehive.controls.scl.DatabaseControlImpl
44  * org.acme.controls.DailyTimerBean=org.apache.beehive.controls.scl.TimerControlImpl
45  *
46  * The manifest is a generated artifact and is not user-editable. Ideally, the apt
47  * environment optimizes the writing of the manifest such that it's only written
48  * to disk when changes occur (allowing external build tools to use the timestamp of
49  * the manifest to determine whether assembly on a client needs to occur).
50  */

51 public class ControlClientManifest
52 {
53     public final static String JavaDoc CLIENT_NAME_PROP = ".client.name";
54     public final static String JavaDoc BEEHIVE_VERSION_PROP = ".beehive.version";
55     public final static String JavaDoc FILE_EXTENSION = ".controls.properties";
56
57     /**
58      * Loads a ControlClientManifest from an existing manifest file.
59      * @param f the manifest file
60      * @throws FileNotFoundException
61      * @throws IOException
62      */

63     public ControlClientManifest( File f ) throws FileNotFoundException, IOException
64     {
65         if ( !f.exists() )
66             throw new FileNotFoundException( "Control manifest file=" + f + " not found");
67
68         FileInputStream fis = new FileInputStream( f );
69         _properties.load( fis );
70
71         String JavaDoc client = _properties.getProperty( CLIENT_NAME_PROP );
72         if ( client == null || client.equals("") )
73             throw new IOException( "Control client manifest missing client name" );
74     }
75
76     /**
77      * Creates a new ControlClientManifest
78      * @param client the fully qualified classname of the control client
79      */

80     public ControlClientManifest( String JavaDoc client )
81     {
82         if ( client == null || client.equals("") )
83             throw new RuntimeException JavaDoc( "Missing or empty client name" );
84
85         _properties.setProperty( CLIENT_NAME_PROP, client );
86     }
87
88     /**
89      * @return the name of the control client in this manifest
90      */

91     public String JavaDoc getControlClient()
92     {
93         return _properties.getProperty( CLIENT_NAME_PROP );
94     }
95
96     /**
97      * Adds a new control type to the manifest
98      * @param intf fully qualified name of the control type
99      * @param impl fully qualified name of the default implementation for the control type
100      */

101     public void addControlType( String JavaDoc intf, String JavaDoc impl )
102     {
103         _properties.setProperty( intf, impl );
104     }
105
106     /**
107      * @return a list of all control types listed in the manifest
108      */

109     public List<String JavaDoc> getControlTypes()
110     {
111         ArrayList<String JavaDoc> l = new ArrayList<String JavaDoc>();
112
113         Set keys = _properties.keySet();
114         for ( Object JavaDoc k : keys )
115         {
116             String JavaDoc propname = (String JavaDoc)k;
117             if ( propname.equals( CLIENT_NAME_PROP ) )
118                 continue;
119
120             l.add( propname );
121         }
122
123         return l;
124     }
125
126     /**
127      * @param controlType
128      * @return the default implementation for the control type listed in the manifest
129      */

130     public String JavaDoc getDefaultImpl( String JavaDoc controlType )
131     {
132         return (String JavaDoc)_properties.get( controlType );
133     }
134
135     /**
136      * Emits the manifest via an apt Filer implementation
137      * @param f an apt Filer
138      * @param pkg the package structure to place the manifest in
139      * @param mf the name of the manifest
140      * @throws IOException
141      */

142     public void emit( Filer f, String JavaDoc pkg, File mf, String JavaDoc csn ) throws IOException
143     {
144         PrintWriter pw = f.createTextFile( Filer.Location.CLASS_TREE, pkg, mf, csn );
145
146         pw.println( "# Apache Beehive Controls client manifest (auto-generated, do not edit!)");
147         Set props = _properties.keySet();
148         for ( Object JavaDoc p : props )
149         {
150             String JavaDoc name = (String JavaDoc)p;
151             String JavaDoc value = _properties.getProperty( name );
152             pw.println( name + "=" + value );
153         }
154
155         pw.flush();
156         pw.close();
157     }
158
159     private Properties _properties = new Properties();
160 }
161
162
Popular Tags