View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.commons.pkg;
21  
22  import java.io.BufferedWriter;
23  import java.io.File;
24  import java.io.IOException;
25  import java.io.OutputStreamWriter;
26  import java.text.SimpleDateFormat;
27  import java.util.Date;
28  import java.util.Properties;
29  
30  /***
31   * Starts the main application.
32   */
33  public class PackageBuilder {
34  
35      //--- Constant(s) ---
36  
37      public static final String CONTROL_FILENAME = "metainfo/control";
38  
39      //--- Data field(s) ---
40  
41  	private static PackageManager packman = new PackageManager();
42  
43  	private static String[] CONTROL_PROPERTIES = new String[] {
44  		"Description",
45  		"Section",
46  		"Package",
47  		"Version",
48  	};
49  
50  	private static String[] CONTROL_PROPERTIES_OPTS = new String[] {
51  		"Authors",
52  		"Depends",
53  	};
54  
55  	private static String[] LIBRARY_PROPERTIES = new String[] {
56  		"Description",
57  		"Section",
58  		"Package",
59  		"Version",
60  	};
61  
62  	private static String[] LIBRARY_PROPERTIES_OPTS = new String[] {
63  		"Authors",
64  		"Class-Path",
65  		"License",
66  		"Long-Description",
67  	};
68  
69  	private static String[] PLUGIN_PROPERTIES = new String[] {
70  		"Description",
71  		"Section",
72  		"Package",
73  		"Version",
74  	};
75  
76  	private static String[] PLUGIN_PROPERTIES_OPTS = new String[] {
77  		"Authors",
78  		"Class-Name",
79  		"Class-Path",
80  		"License",
81  		"Long-Description",
82  	};
83  
84      //--- Method(s) ---
85  
86      public static void main(final String[] argv) throws IOException
87      {
88  		packman.read(new File(CONTROL_FILENAME));
89  		
90  		if (argv.length == 2 && argv[0].equals("-p")) {
91  			printProperties(argv[1], PLUGIN_PROPERTIES, 
92  							PLUGIN_PROPERTIES_OPTS);
93  		}
94  		else if (argv.length == 2 && argv[0].equals("-l")) {
95  			printProperties(argv[1], LIBRARY_PROPERTIES, 
96  							LIBRARY_PROPERTIES_OPTS);
97  		}
98  		else if (argv.length == 3 && argv[0].equals("-c")) {
99  			printControl(argv[1], argv[2]);
100 		}
101 		else {
102 			exit("usage: java xnap.pkg.PackageBuilder (-p package |-c package zip)");
103 		}
104 
105 		System.exit(0);
106     }
107 
108 	public static void printProperties(String packageName, String[] keys, 
109 									   String[] optKeys)
110 		throws IOException
111 	{
112 		PackageInfo info = packman.getPackage(packageName);
113 		if (info == null) {
114 			exit("no such package: " + packageName);
115 		}
116 		Properties p = put(info.getProperties(), keys, optKeys);
117 		p.put("Installed", "install ok installed");
118 		p.put("Release-Nr", getReleaseNr());
119 		print(p);
120 	}
121 
122 	public static void printControl(String packageName, String zipFilename)
123 		throws IOException
124 	{
125 		File zip = new File(zipFilename);
126 
127 		PackageInfo info = packman.getPackage(packageName);
128 		if (info == null) {
129 			exit("no such package: " + packageName);
130 		}
131 		Properties p = put(info.getProperties(), CONTROL_PROPERTIES, 
132 						   CONTROL_PROPERTIES_OPTS);
133 		p.put("Size", zip.length() + "");
134 		p.put("Filename", zip.getName());
135 		p.put("Release-Nr", getReleaseNr());
136 		//p.put("MD5Sum")
137 		print(p);
138 	}
139 
140 	public static String getReleaseNr()
141 	{	
142 		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd01");
143 		return df.format(new Date());
144 	}
145 
146 	public static Properties put(Properties p, String[] keys, String[] optKeys)
147 		throws IOException
148 	{
149 		Properties props = new Properties();
150 		for (int i = 0; i < keys.length; i++) {
151 			if (p.getProperty(keys[i]) == null) {
152 				exit("missing key: " + keys[i]);
153 			}
154 			props.put(keys[i], p.getProperty(keys[i]));
155 		}
156 		for (int i = 0; i < optKeys.length; i++) {
157 			if (p.getProperty(optKeys[i]) != null) {
158 				props.put(optKeys[i], p.getProperty(optKeys[i]));
159 			}
160 		}
161 		return props;
162 	}
163 
164 	public static void print(Properties p) 
165 		throws  IOException
166 	{
167 		PackageInfoWriter.write
168 			(new BufferedWriter(new OutputStreamWriter(System.out)), p);
169 	}
170 
171 	public static void exit(String error)
172 	{
173 		System.err.println(error);
174 		System.exit(1);
175 	}
176 
177 }