001    /*
002     * Copyright 2005 Stephen J. McConnell
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.station.builder;
020    
021    import java.io.OutputStream;
022    import java.io.OutputStreamWriter;
023    import java.io.Writer;
024    import java.io.IOException;
025    import java.util.Properties;
026    
027    import net.dpml.station.info.RegistryDescriptor;
028    import net.dpml.station.info.RegistryDescriptor.Entry;
029    import net.dpml.station.info.ApplicationDescriptor;
030    import net.dpml.station.info.StartupPolicy;
031    
032    import net.dpml.lang.ValueDirective;
033    
034    /**
035     * Test example application sources.
036     *
037     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
038     * @version 1.0.3
039     */
040    public final class RegistryWriter
041    {
042        private static final String XML_HEADER = 
043          "<?xml version=\"1.0\"?>";
044        
045        private static final String APPLICATION_SCHEMA_URN = "link:xsd:dpml/station/dpml-station-info#1.0";
046        
047       /**
048        * Externalize the part to XML.
049        * @param descriptor the registry descriptor
050        * @param output the output stream 
051        * @param pad offset padding to be applied when writing data to the stream
052        * @exception IOException if an IO error occurs
053        */
054        public void writeRegistryDescriptor( 
055          RegistryDescriptor descriptor, OutputStream output, String pad ) throws IOException
056        {
057            final Writer writer = new OutputStreamWriter( output );
058            writer.write( XML_HEADER );
059            writer.write( "\n" );
060            writer.write( "\n<registry xmlns=\"" + APPLICATION_SCHEMA_URN + "\">" );
061            writer.write( "\n" );
062            
063            Entry[] entries = descriptor.getEntries();
064            for( int i=0; i<entries.length; i++ )
065            {
066                Entry entry = entries[i];
067                writeEntry( writer, entry, "  " );
068            }
069            writer.write( "\n" );
070            writer.write( "\n</registry>" );
071            writer.write( "\n" );
072            writer.flush();
073            output.close();
074        }
075    
076        private void writeEntry( Writer writer, Entry entry, String pad ) throws IOException
077        {
078            String key = entry.getKey();
079            ApplicationDescriptor descriptor = entry.getApplicationDescriptor();
080            String title = descriptor.getTitle();
081            StartupPolicy policy = descriptor.getStartupPolicy();
082            String policyValue = policy.getName();
083            
084            writer.write( "\n" + pad + "<app key=\"" + key + "\"" );
085            if( null != title )
086            {
087                writer.write( " title=\"" + title + "\"" );
088            }
089            writer.write( " policy=\"" + policyValue + "\">" );
090            writeJvm( writer, descriptor, pad + "  " );
091            writeCodebase( writer, descriptor, pad + "  " );
092            writer.write( "\n" + pad + "</app>" );
093            writer.write( "\n" );
094        }
095        
096        private void writeJvm( Writer writer, ApplicationDescriptor descriptor, String pad ) throws IOException
097        {
098            writer.write( "\n" + pad + "<jvm" );
099            String base = descriptor.getBasePath();
100            if( null != base )
101            {
102                writer.write( " basedir=\"" + base + "\">" );
103            }
104            else
105            {
106                writer.write( ">" );
107            }
108            int startup = descriptor.getStartupTimeout();
109            int shutdown = descriptor.getShutdownTimeout();
110            writer.write( "\n" + pad + "  <startup>" + startup + "</startup>" );
111            writer.write( "\n" + pad + "  <shutdown>" + shutdown + "</shutdown>" );
112            Properties properties = descriptor.getSystemProperties();
113            String[] names = (String[]) properties.keySet().toArray( new String[0] );
114            if( names.length > 0 )
115            {
116                writer.write( "\n" + pad + "  <properties>" );
117                for( int i=0; i<names.length; i++ )
118                {
119                    String name = names[i];
120                    String value = properties.getProperty( name );
121                    writer.write( "\n" + pad + "    <property name=\"" + name + "\" value=\"" + value + "\"/>" );
122                }
123                writer.write( "\n" + pad + "  </properties>" );
124            }
125            writer.write( "\n" + pad + "</jvm>" );
126        }
127        
128        private void writeCodebase( Writer writer, ApplicationDescriptor descriptor, String pad ) throws IOException
129        {
130            String uri = descriptor.getCodeBaseURISpec();
131            writer.write( "\n" + pad + "<codebase uri=\"" + uri + "\"" );
132            ValueDirective[] values = descriptor.getValueDirectives();
133            if( values.length == 0 )
134            {
135                writer.write( "/>" );
136            }
137            else
138            {
139                writeValueDirectives( writer, values, pad + "  " );
140                writer.write( "\n" + pad + "</codebase>" );
141            }
142        }
143    
144       /**
145        * Write a value directive array.
146        * @param writer the stream writer
147        * @param values the array of value directives
148        * @param pad character offset
149        * @exception IOException if an IO error occurs
150        */
151        protected void writeValueDirectives( Writer writer, ValueDirective[] values, String pad ) throws IOException
152        {
153            for( int i=0; i<values.length; i++ )
154            {
155                ValueDirective value = values[i];
156                writeValueDirective( writer, value, pad );
157            }
158        }
159        
160       /**
161        * Write a single value directive.
162        * @param writer the stream writer
163        * @param value the value directive
164        * @param pad character offset
165        * @exception IOException if an IO error occurs
166        */
167        protected void writeValueDirective( Writer writer, ValueDirective value, String pad ) throws IOException
168        {
169            String method = value.getMethodName();
170            String target = value.getTargetExpression();
171            
172            writer.write( "\n" + pad + "<param" );
173            if( null != target )
174            {
175                writer.write( " class=\"" + target + "\"" );
176            }
177            if( null != method )
178            {
179                writer.write( " method=\"" + method  + "\"" );
180            }            
181            if( value.isCompound() )
182            {
183                writer.write( ">" );
184                ValueDirective[] values = value.getValueDirectives();
185                writeValueDirectives( writer, values, pad + "  " );
186                writer.write( "\n" + pad + "</param>" );
187            }
188            else
189            {
190                String v = value.getBaseValue();
191                if( null != value )
192                {
193                    writer.write( " value=\"" + v  + "\"" );
194                }
195                writer.write( "/>" );
196            }
197        }
198    }