View Javadoc

1   /*
2    * Copyright (c) 2003-2004, Henri Yandell
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or 
6    * without modification, are permitted provided that the 
7    * following conditions are met:
8    * 
9    * + Redistributions of source code must retain the above copyright notice, 
10   *   this list of conditions and the following disclaimer.
11   * 
12   * + Redistributions in binary form must reproduce the above copyright notice, 
13   *   this list of conditions and the following disclaimer in the documentation 
14   *   and/or other materials provided with the distribution.
15   * 
16   * + Neither the name of OSJava nor the names of its contributors 
17   *   may be used to endorse or promote products derived from this software 
18   *   without specific prior written permission.
19   * 
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
23   * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
30   * POSSIBILITY OF SUCH DAMAGE.
31   */
32  package org.osjava.payload;
33  
34  import java.io.BufferedReader;
35  import java.io.IOException;
36  import java.io.StringReader;
37  
38  import java.util.ArrayList;
39  import java.util.Iterator;
40  import java.util.List;
41  import java.util.Properties;
42  
43  /***
44   * Holds the payload.properties file. 
45   * Said file relies on order and allows duplicates so is not 
46   * a standard Properties file, nor wants to be accessed 
47   * via that API.
48   *
49   *   org.osjava.payload=true
50   *   org.osjava.payload.paylet=com.example.ExamplePaylet arg1 arg2
51   */
52  public class PayloadConfiguration {
53  
54      private static final String PAYLET = "org.osjava.payload.paylet";
55      private static final String ENDS_WITH = "org.osjava.payload.interpolate.endsWith";
56      private static final String MATCHES = "org.osjava.payload.interpolate.matches";
57      private static final String ARCHIVE_ENDS_WITH = "org.osjava.payload.interpolate.archive.endsWith";
58      private static final String ARCHIVE_MATCHES = "org.osjava.payload.interpolate.archive.matches";
59  
60      public static final PayloadConfiguration DEFAULT = new PayloadConfiguration( 
61          "org.osjava.payload=true\n" +
62          "org.osjava.payload.interpolate.endsWith=xml\n" +
63          "org.osjava.payload.interpolate.endsWith=jcml\n" +
64          "org.osjava.payload.interpolate.endsWith=properties\n" +
65          "org.osjava.payload.interpolate.endsWith=txt\n" +
66          "org.osjava.payload.interpolate.endsWith=conf\n"
67      );
68  
69      private List fileMatches = new ArrayList();
70      private List fileEndsWith = new ArrayList();
71      private List archiveEndsWith = new ArrayList();
72      private List archiveMatches = new ArrayList();
73      private List paylets = new ArrayList();
74  
75      public PayloadConfiguration(String txt) {
76  
77          try {
78  
79              BufferedReader rdr = new BufferedReader(new StringReader(txt));
80              String line = "";
81  
82              while( (line = rdr.readLine()) != null) {
83                  if(line.startsWith(ENDS_WITH)) {
84                      int idx = line.indexOf("=");
85                      if(idx != -1) {
86  if(PayloadExtractor.DEBUG) System.out.println("Adding endsWith rule: "+line.substring(idx+1));
87                          this.fileEndsWith.add(line.substring(idx+1));
88                      }
89                  }
90      
91                  if(line.startsWith(ARCHIVE_ENDS_WITH)) {
92                      int idx = line.indexOf("=");
93                      if(idx != -1) {
94  if(PayloadExtractor.DEBUG) System.out.println("Adding archiveEndsWith rule: "+line.substring(idx+1));
95                          this.archiveEndsWith.add(line.substring(idx+1));
96                      }
97                  }
98      
99                  if(line.startsWith(MATCHES)) {
100                     int idx = line.indexOf("=");
101                     if(idx != -1) {
102 if(PayloadExtractor.DEBUG) System.out.println("Adding matches rule: "+line.substring(idx+1));
103                         this.fileMatches.add(line.substring(idx+1));
104                     }
105                 }
106     
107                 if(line.startsWith(ARCHIVE_MATCHES)) {
108                     int idx = line.indexOf("=");
109                     if(idx != -1) {
110 if(PayloadExtractor.DEBUG) System.out.println("Adding archiveMatches rule: "+line.substring(idx+1));
111                         this.archiveMatches.add(line.substring(idx+1));
112                     }
113                 }
114     
115                 try {
116                     if(line.startsWith(PAYLET)) {
117                         int idx = line.indexOf("=");
118                         if(idx != -1) {
119 if(PayloadExtractor.DEBUG) System.out.println("Loading paylet: "+line.substring(idx+1));
120                             String[] words = line.substring(idx+1).split(" ");
121                             Paylet paylet = (Paylet) Class.forName(words[0]).newInstance();
122                             String[] args = new String[words.length - 1];
123                             System.arraycopy( words, 1, args, 0, args.length );
124                             paylet.setArgs(args);
125                             this.paylets.add( paylet );
126                         }
127                     }
128                 } catch(ClassNotFoundException cnfe) {
129                     // ? throw ParsingException?
130                     cnfe.printStackTrace();
131                 } catch(InstantiationException ie) {
132                     // ? throw ParsingException?
133                     ie.printStackTrace();
134                 } catch(IllegalAccessException iae) {
135                     // ? throw ParsingException?
136                     iae.printStackTrace();
137                 }
138             }
139         } catch(IOException ioe) {
140             // ? throw ParsingException?
141             ioe.printStackTrace();
142         }
143     }
144 
145     public List getArchiveEndsWith() { return this.archiveEndsWith; }
146     public List getArchiveMatches() { return this.archiveMatches; }
147     public List getFileEndsWith() { return this.fileEndsWith; }
148     public List getFileMatches() { return this.fileMatches; }
149     public List getPaylets() { return this.paylets; }
150 
151     public String toString() {
152         StringBuffer buffer = new StringBuffer();
153 
154         buffer.append("PayloadConfiguration: ");
155         buffer.append("archiveEndsWith: ");
156         buffer.append(this.archiveEndsWith);
157         buffer.append(", archiveMatches: ");
158         buffer.append(this.archiveMatches);
159         buffer.append(", fileEndsWith: ");
160         buffer.append(this.fileEndsWith);
161         buffer.append(", fileMatches: ");
162         buffer.append(this.fileMatches);
163         buffer.append(", paylets: ");
164         buffer.append(this.paylets);
165 
166         return buffer.toString();
167     }
168 
169 }