View Javadoc

1   /*
2    * 
3    * 
4    */
5   package earutils.expander;
6   
7   import java.util.*;
8   
9   /***
10   * 
11   * @author Sean C. Sullivan
12   *
13   * 
14   * 
15   */
16  public class Main
17  {
18  	private String filename;
19  	private String outputDirectory;
20  	private boolean verbose = false;
21  	private boolean readyToRun = false;
22  	private Set extensions = new java.util.HashSet();
23  	
24  	public Main()
25  	{
26  		
27  	}
28  
29  	public int execute()
30  	{
31  
32  		int exitCode = 1;
33  		
34  		try
35  		{
36  			Expander expander = new Expander();
37  
38  			expander.setExpansionExtensions(extensions);
39  			
40  			expander.setOutputPath(outputDirectory);
41  			
42  			expander.addExpanderListener(new SimpleExpanderListener(verbose));
43  			
44  			expander.expand(filename);
45  			
46  			exitCode = 0;
47  		}
48  		catch (Exception ex)
49  		{
50  			ex.printStackTrace();
51  			exitCode = 1;
52  		}
53  		
54  		return exitCode;
55  		
56  	}
57  	
58  	public boolean isReadyToRun()
59  	{
60  		return readyToRun;
61  	}
62  	
63  	protected void processArgs(String[] args)
64  	{
65  		readyToRun = true;
66  		
67  		try
68  		{
69  			for (int i = 0; i < args.length; i++)
70  			{
71  				String arg = args[i];
72  				
73  				if (arg.equals("-help"))
74  				{
75  					readyToRun = false;
76  					printUsage();
77  					return;
78  				}
79  				else if (arg.equals("-outputdir"))
80  				{
81  					outputDirectory = args[i + 1];
82  					i++;
83  				}
84  				else if (arg.equals("-filename"))
85  				{
86  					filename = args[i + 1];
87  					i++;
88  				}
89  				else if (arg.equals("-verbose"))
90  				{
91  					verbose = true;
92  				}
93  				else if (arg.equals("-expandextension"))
94  				{
95  					String ext = args[i + 1];
96  					this.extensions.add(ext);
97  					i++;
98  				}
99  				else 
100 				{
101 					System.err.println("unexpected argument: " + arg);
102 					readyToRun = false;
103 				}
104 			}
105 			
106 			if ( (outputDirectory == null) || (filename == null) )
107 			{
108 				readyToRun = false;
109 			}
110 		}
111 		catch (Exception ex)
112 		{
113 			System.err.println("Error while processing command line arguments");
114 			ex.printStackTrace();
115 			readyToRun = false;
116 		}
117 		
118 	}
119 	
120 	static protected void printUsage()
121 	{
122 		System.out.println("earutils.expander.Main -filename foo.ear -outputdir c://output -expandextension .war -verbose");
123 	}
124 	
125 	public static void main(String[] args)
126 	{
127 		int exitCode = 0;
128 		
129 		try
130 		{
131 			Main m = new Main();
132 			m.processArgs(args);
133 			if (m.isReadyToRun())
134 			{
135 				exitCode = m.execute();
136 			}
137 			else
138 			{
139 				printUsage();
140 				exitCode = 1;
141 			}
142 		}
143 		catch (Exception ex)
144 		{
145 			ex.printStackTrace();
146 			exitCode = 1;
147 		}
148 		System.exit(exitCode);
149 	}
150 }