View Javadoc

1   package net.sf.jpkgmk;
2   
3   import java.io.BufferedReader;
4   import java.io.BufferedWriter;
5   import java.io.File;
6   import java.io.FileInputStream;
7   import java.io.FileWriter;
8   import java.io.IOException;
9   import java.io.InputStream;
10  import java.io.InputStreamReader;
11  import java.io.Writer;
12  import java.util.ArrayList;
13  import java.util.Iterator;
14  import java.util.List;
15  
16  import net.sf.jpkgmk.util.StreamUtil;
17  
18  /**
19   * Creates the request file for a solaris package
20   * @author gommma (gommma AT users.sourceforge.net)
21   * @author Last changed by: $Author: gommma $
22   * @version $Revision: 3 $ $Date: 2008-08-20 22:22:44 +0200 (Mi, 20 Aug 2008) $
23   * @since 1.0
24   */
25  public class RequestCreator {
26  
27      private List<RequestEntry> requestEntryList = new ArrayList<RequestEntry>();
28      
29      
30  	public RequestCreator(InputStream requestEntries) throws IOException
31  	{
32          List<RequestEntry> entries = new RequestEntryParser().parse(requestEntries);
33  		this.requestEntryList.addAll(entries);
34  	}
35  	
36  	public void addEntry(RequestEntry entry)
37      {
38          if(entry == null) {
39              throw new NullPointerException("The parameter 'entry' must not be null");
40          }
41          requestEntryList.add(entry);
42      }
43      
44      public void create(File target) throws IOException
45      {
46          String request = create();
47          BufferedWriter writer = new BufferedWriter(new FileWriter(target));
48          try {
49              writer.write(request);
50          }
51          finally {
52              StreamUtil.tryCloseStream(writer);
53          }
54      }
55  
56      public void create(Writer target) throws IOException
57      {
58          String request = create();
59          target.write(request);
60      }
61  
62      
63  	private String create()
64  	{
65  		StringBuffer sb = new StringBuffer();
66  		sb.append("#!/bin/sh").append(PkgInfo.LINE_SEPARATOR);
67  		sb.append("# bla bla bla").append(PkgInfo.LINE_SEPARATOR);
68  		sb.append(". /usr/lib/sh.lib/require").append(PkgInfo.LINE_SEPARATOR);
69  		sb.append("usage=\"\"").append(PkgInfo.LINE_SEPARATOR);
70  		sb.append("require message_handling \"$prog\" \"$usage\"").append(PkgInfo.LINE_SEPARATOR);
71          
72  		// Create the dynamic request entries
73          for(Iterator<RequestEntry> iter=requestEntryList.iterator(); iter.hasNext(); ) {
74              RequestEntry entry = (RequestEntry)iter.next();
75              sb.append(entry.getEntry());
76          }
77          
78  	    sb.append("INIT_HEAP_SIZE=`expr $MAX_HEAP_SIZE / 4`").append(PkgInfo.LINE_SEPARATOR);
79  	    sb.append("HOSTNM=`uname -n`").append(PkgInfo.LINE_SEPARATOR);
80  	    sb.append("FQDN=`/usr/sbin/nslookup $HOSTNM|nawk '/Name/ {print $2}' | tail -1`").append(PkgInfo.LINE_SEPARATOR);
81  	    sb.append("echo \"VIRTUAL_REQD=\\\"$VIRTUAL_REQD\\\"\" >> $1").append(PkgInfo.LINE_SEPARATOR);
82  	    sb.append("echo \"INIT_HEAP_SIZE=\\\"$INIT_HEAP_SIZE\\\"\" >> $1").append(PkgInfo.LINE_SEPARATOR);
83  	    sb.append("echo \"FQDN=\\\"$FQDN\\\"\" >> $1").append(PkgInfo.LINE_SEPARATOR);
84  		return sb.toString();
85  	}
86  	
87      /**
88       * Enum for the different types of request entries
89       */
90  	public static class RequestEntryType
91      {
92          public static final RequestEntryType DIRECTORY = new RequestEntryType("d");
93          public static final RequestEntryType LIST = new RequestEntryType("l");
94          public static final RequestEntryType VARIABLE = new RequestEntryType("v");
95          public static final RequestEntryType PASSWORD = new RequestEntryType("p");
96  
97          private String typeKey;
98          private RequestEntryType(String typeKey)
99          {
100             this.typeKey=typeKey;
101         }
102         public String getTypeKey()
103         {
104             return this.typeKey;
105         }
106     }
107     
108     
109 	public abstract static class RequestEntry
110 	{
111 		private static final String COMMENT_HEADER = "###################################";
112 		private static final String COMMENT_HEADER2 = "# -------------------------";
113 		
114 		protected String varName;
115         /**
116          * Default value of the variable
117          */
118 		protected String varValue;
119 		protected String varName111;
120 		protected String description;
121 		protected RequestEntryType type;
122 		
123 		
124 		public RequestEntry(String varName, String varValue, String varName111, String description, RequestEntryType type) {
125             super();
126             this.varName = varName;
127             this.varValue = varValue;
128             this.varName111 = varName111;
129             this.description = description;
130             this.type = type;
131         }
132 
133         public RequestEntryType getType()
134         {
135             return this.type;
136         }
137         
138         protected abstract String getDataSection();
139 		
140 		private String create()
141 		{
142 			StringBuffer sb = new StringBuffer();
143 			
144 			sb.append(COMMENT_HEADER).append(PkgInfo.LINE_SEPARATOR);
145 			sb.append("# Get " + description).append(PkgInfo.LINE_SEPARATOR);
146 			sb.append(COMMENT_HEADER2).append(PkgInfo.LINE_SEPARATOR);
147 			
148             String dataSection = getDataSection();
149             sb.append(dataSection).append(PkgInfo.LINE_SEPARATOR);
150             
151             String commonDataEnd = "echo " + varName + "=\\\"$" + varName + "\\\" >> $1";
152             if(dataSection != null) {
153                 sb.append(commonDataEnd).append(PkgInfo.LINE_SEPARATOR);
154             }
155             sb.append(PkgInfo.LINE_SEPARATOR);
156             
157 			return sb.toString();
158 		}
159 		
160 		public String getEntry()
161 		{
162 			return create();
163 		}
164         
165         public String getVarName() {
166             return this.varName;
167         }
168 
169         public String getVarName111() {
170             return this.varName111;
171         }
172 
173         /**
174          * Adds the definition of the variable as string to the given string buffer.
175          * @param sb
176          * @param varName
177          * @param varValue
178          */
179         public void addDefLine(StringBuffer sb, String varName, String varValue) {
180             sb.append(varName + "=\"" + varValue + "\"").append(PkgInfo.LINE_SEPARATOR);
181         }
182 
183         public void addUpdate111Line(StringBuffer sb, String varName, String varName111, String mapKey) {
184             sb.append("if [ -z \"$" + varName + "\" ] ; ").append(PkgInfo.LINE_SEPARATOR);
185             sb.append("then " + varName + "=$DEF" + varName + " ; 111 -u reference " + mapKey + " " + varName111 + " $" + varName +" ;").append(PkgInfo.LINE_SEPARATOR);
186             sb.append("fi;").append(PkgInfo.LINE_SEPARATOR);
187         }
188 
189 	}
190 	
191     
192     
193 	public static class DefaultRequestEntry extends RequestEntry
194 	{
195 		
196 		public DefaultRequestEntry(String varName, String varValue, String varName111, String description, RequestEntryType type) {
197             super(varName, varValue, varName111, description, type);
198         }
199 
200         @Override
201 		protected String getDataSection() {
202 			StringBuffer sb = new StringBuffer();
203             addDefLine(sb, "DEF" + varName, varValue);
204 			sb.append(varName + "=`111 reference -p \"Enter " + description + " [$DEF" + varName + "]:\"  $PKG/$VERSION " + varName111 + "`").append(PkgInfo.LINE_SEPARATOR);
205             super.addUpdate111Line(sb, varName, varName111, "$PKG/$VERSION");
206 //            if(type.equals("d")) {
207 //                sb.append("[ ! -d \"$" + varName + "\" ] && msg_fatal \"" + varName + " '$" + varName + "' is not a valid directory\"").append(PackageInfo.LINE_SEPARATOR);
208 //            }
209                 
210 			return sb.toString();
211 		}
212 	}
213 
214     public static class ListRequestEntry extends RequestEntry
215     {
216 
217         private String optionListString;
218         
219         public ListRequestEntry(String varName, String varValue, String varName111, String description, RequestEntryType type, String optionList) {
220             super(varName, varValue, varName111, description, type);
221             this.optionListString = optionList;
222         }
223 
224         public ListRequestEntry(String varName, String varValue, String varName111, String description, RequestEntryType type, List<String> options) {
225             super(varName, varValue, varName111, description, type);
226             initOptionListString(options);
227         }
228         
229         private void initOptionListString(List<String> options) {
230             String optionListString = "";
231             for (int i = 0; i < options.size(); i++) {
232                 String option = options.get(i);
233                 optionListString += option;
234                 if(i<options.size()-1) {
235                     optionListString += "|";
236                 }
237             }
238         }
239 
240         @Override
241 		protected String getDataSection() {
242             StringBuffer sb = new StringBuffer();
243             String listVarDef = "DEF" + varName + "LIST";
244             String varDef = "DEF" + varName;
245             super.addDefLine(sb, varDef, varValue);
246             super.addDefLine(sb, listVarDef, optionListString);
247             sb.append(varName + "=`111 reference -p \"Enter " + description + " [$" + listVarDef + "][" + varValue + "]:\"  $PKG/$VERSION " + varName111 + "`").append(PkgInfo.LINE_SEPARATOR);
248             super.addUpdate111Line(sb, varName, varName111, "$PKG/$VERSION");
249             return sb.toString();
250         }
251     }
252     
253     
254     private static class PasswordRequestEntry extends RequestEntry
255     {
256         public PasswordRequestEntry(String varName, String varValue, String varName111, String description, RequestEntryType type) {
257             super(varName, varValue, varName111, description, type);
258         }
259 
260         @Override
261 		protected String getDataSection() {
262             StringBuffer sb = new StringBuffer();
263             addDefLine(sb, "DEF" + varName, varValue);
264             sb.append(varName + "=`111 reference -p \"Enter " + description + " [$DEF" + varName + "]:\"  $PKG/$VERSION " + varName111 + "`").append(PkgInfo.LINE_SEPARATOR);
265             super.addUpdate111Line(sb, varName, varName111, "$PKG");
266             return sb.toString();
267         }
268     }
269     
270     
271     
272     public static class RequestEntryParser
273     {
274         public RequestEntryParser()
275         {
276         }
277         
278         public List<RequestEntry> parse(File requestEntryList) throws IOException
279         {
280             InputStream input = new FileInputStream(requestEntryList);
281             return parse(input);
282         }
283 
284         public List<RequestEntry> parse(InputStream input) throws IOException {
285         	if (input == null) {
286 				throw new NullPointerException("The parameter 'input' must not be null");
287 			}
288         	
289             BufferedReader reader = new BufferedReader(new InputStreamReader(input));
290             List<RequestEntry> result = new ArrayList<RequestEntry>();
291             try {
292                 String line = null;
293                 while((line=reader.readLine()) != null) {
294                     RequestEntry entry = parseLine(line);
295                     result.add(entry);
296                 }
297             }
298             finally {
299                 StreamUtil.tryCloseStream(reader);
300             }
301             return result;
302         }
303 
304         private RequestEntry parseLine(String line) 
305         {
306             String[] values = line.split(",");
307             String varNameDef = values[0];
308             String varName111 = values[1];
309             String descr = values[2];
310 //            String somethingDoNotKnowAndUse = values[3];
311             String type = values[4];
312 
313             String list = null;
314             String varValue = null;
315             // Get default value and optionList
316             if(RequestEntryType.LIST.getTypeKey().equals(type) && values.length >= 7) {
317                 list = values[5];
318                 varValue = values[6];
319             }
320             else if(values.length >= 6) {
321                 varValue = values[5];
322             }
323             
324             
325             if(RequestEntryType.DIRECTORY.getTypeKey().equals(type)){
326                 return new DefaultRequestEntry(varNameDef, varValue, varName111, descr, RequestEntryType.DIRECTORY);
327             }
328             else if(RequestEntryType.VARIABLE.getTypeKey().equals(type)){
329                 return new DefaultRequestEntry(varNameDef, varValue, varName111, descr, RequestEntryType.VARIABLE);
330             }
331             else if(RequestEntryType.PASSWORD.getTypeKey().equals(type)){
332                 return new PasswordRequestEntry(varNameDef, varValue, varName111, descr, RequestEntryType.PASSWORD);
333             }
334             else if(RequestEntryType.LIST.getTypeKey().equals(type)){
335                 return new ListRequestEntry(varNameDef, varValue, varName111, descr, RequestEntryType.LIST, list);
336             }
337             else {
338                 throw new IllegalStateException("The given type '" + type + "' is not known.");
339             }
340         }
341         
342     }
343 }