KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > tools > Tool


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.tools;
18
19 import org.alfresco.service.ServiceRegistry;
20 import org.alfresco.service.cmr.security.AuthenticationService;
21 import org.alfresco.util.ApplicationContextHelper;
22 import org.springframework.context.ApplicationContext;
23
24
25 /**
26  * Abstract Tool Implementation
27  *
28  * @author David Caruana
29  */

30 public abstract class Tool
31 {
32     /** Tool Context */
33     private ToolContext toolContext;
34     /** Spring Application Context */
35     private ApplicationContext appContext;
36     /** Repository Service Registry */
37     private ServiceRegistry serviceRegistry;
38     
39     
40     /**
41      * Process Tool Arguments
42      *
43      * @param args the arguments
44      * @return the tool context
45      * @throws ToolException
46      */

47     /*package*/ ToolContext processArgs(String JavaDoc[] args)
48         throws ToolException
49     {
50         return new ToolContext();
51     }
52     
53     /**
54      * Display Tool Help
55      */

56     /*package*/ void displayHelp()
57     {
58         System.out.println("Sorry. Help is not available.");
59     }
60
61     /**
62      * Perform Tool Behaviour
63      *
64      * @throws ToolException
65      */

66     /*package*/ abstract void execute()
67         throws ToolException;
68
69     /**
70      * Get the tool name
71      *
72      * @return the tool name
73      */

74     /*package*/ abstract String JavaDoc getToolName();
75
76     /**
77      * Get the Application Context
78      *
79      * @return the application context
80      */

81     /*package*/ final ApplicationContext getApplicationContext()
82     {
83         return appContext;
84     }
85     
86     /**
87      * Get the Repository Service Registry
88      *
89      * @return the service registry
90      */

91     /*package*/ final ServiceRegistry getServiceRegistry()
92     {
93         return serviceRegistry;
94     }
95     
96     /**
97      * Log message
98      *
99      * @param msg message to log
100      */

101     /*package*/ final void log(String JavaDoc msg)
102     {
103         if (toolContext.isQuiet() == false)
104         {
105             System.out.println(msg);
106         }
107     }
108
109     /**
110      * Log Verbose message
111      *
112      * @param msg message to log
113      */

114     /*package*/ final void logVerbose(String JavaDoc msg)
115     {
116         if (toolContext.isVerbose())
117         {
118             log(msg);
119         }
120     }
121     
122     /**
123      * Tool entry point
124      *
125      * @param args the tool arguments
126      */

127     /*package*/ final void start(String JavaDoc[] args)
128     {
129         try
130         {
131             // Process tool arguments
132
toolContext = processArgs(args);
133             toolContext.validate();
134     
135             try
136             {
137                 if (toolContext.isHelp())
138                 {
139                     // Display help, if requested
140
displayHelp();
141                 }
142                 else
143                 {
144                     // Perform Tool behaviour
145
log(getToolName());
146                     initialiseRepository();
147                     login();
148                     execute();
149                     log(getToolName() + " successfully completed.");
150                 }
151                 System.exit(0);
152             }
153             catch (ToolException e)
154             {
155                 displayError(e);
156                 System.exit(-1);
157             }
158         }
159         catch(ToolException e)
160         {
161             System.out.println(e.getMessage());
162             System.out.println();
163             displayHelp();
164             System.exit(-1);
165         }
166         catch (Throwable JavaDoc e)
167         {
168             System.out.println("The following error has occurred:");
169             System.out.println(e.getMessage());
170             e.printStackTrace();
171             System.exit(-1);
172         }
173     }
174
175     /**
176      * Login to Repository
177      */

178     private void login()
179     {
180         // TODO: Replace with call to ServiceRegistry
181
AuthenticationService auth = (AuthenticationService) serviceRegistry.getAuthenticationService();
182         auth.authenticate(toolContext.getUsername(), toolContext.getPassword().toCharArray());
183         log("Connected as " + toolContext.getUsername());
184     }
185     
186     /**
187      * Initialise the Repository
188      */

189     private void initialiseRepository()
190     {
191         appContext = ApplicationContextHelper.getApplicationContext();
192         serviceRegistry = (ServiceRegistry) appContext.getBean(ServiceRegistry.SERVICE_REGISTRY);
193     }
194     
195     /**
196      * Display Error Message
197      *
198      * @param e exception
199      */

200     private void displayError(Throwable JavaDoc e)
201     {
202         System.out.println(e.getMessage());
203         if (toolContext != null && toolContext.isVerbose())
204         {
205             e.printStackTrace();
206         }
207     }
208
209 }
210
Popular Tags