1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
130 cnfe.printStackTrace();
131 } catch(InstantiationException ie) {
132
133 ie.printStackTrace();
134 } catch(IllegalAccessException iae) {
135
136 iae.printStackTrace();
137 }
138 }
139 } catch(IOException ioe) {
140
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 }