KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > services > psmlmanager > PsmlImporter


1 /*
2  * Copyright 2000-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16  
17 package org.apache.jetspeed.services.psmlmanager;
18
19 import java.util.Iterator JavaDoc;
20
21 import org.apache.turbine.services.TurbineServices;
22
23 // Jetspeed Security service
24
import org.apache.jetspeed.services.JetspeedSecurity;
25 import org.apache.jetspeed.services.security.JetspeedSecurityException;
26 import org.apache.jetspeed.services.security.UnknownUserException;
27 import org.apache.jetspeed.om.security.JetspeedUser;
28 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
29 import org.apache.jetspeed.services.logging.JetspeedLogger;
30
31 // Profile and ProfileLocator interface
32
import org.apache.jetspeed.services.PsmlManager;
33 import org.apache.jetspeed.om.profile.QueryLocator;
34
35 import org.apache.turbine.util.TurbineConfig;
36
37 /**
38  * Reads all PSML files from the file system and imports them into PSML DB
39  *
40  * @author <a HREF="mailto:taylor@apache.org">David Sean Taylor</a>
41  * @version $Id: PsmlImporter.java,v 1.16 2004/02/23 03:32:51 jford Exp $
42  */

43 public class PsmlImporter
44 {
45     /**
46      * Static initialization of the logger for this class
47      */

48     private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(PsmlImporter.class.getName());
49     
50     protected boolean check = true;
51
52     public PsmlImporter()
53     {
54     }
55
56     public static void main(String JavaDoc args[])
57     {
58         System.out.println("***** PSML Importer *****");
59         boolean checkImport = true;
60         
61         //
62
// initialize and bootstrap services
63
//
64
try
65         {
66             String JavaDoc root = "./webapp";
67             String JavaDoc properties = "/WEB-INF/conf/TurbineResources.properties";
68             if (args.length > 0)
69             {
70                 if (args[0].equalsIgnoreCase("true"))
71                     checkImport = true;
72                 else
73                     checkImport = false;
74             }
75             if (args.length > 1)
76             {
77                 root = args[1];
78             }
79             if (args.length > 2)
80             {
81                 properties = args[2];
82             }
83             TurbineConfig config = new TurbineConfig( root, properties);
84             config.init();
85         }
86         catch (Exception JavaDoc e)
87         {
88             String JavaDoc msg = "PSML Importer: error initializing Turbine configuration";
89             logger.error(msg, e);
90             System.out.println(msg);
91             e.printStackTrace();
92             System.exit(0);
93         }
94
95         //
96
// get a handle to the exporter service
97
//
98
PsmlManagerService exporterService = null;
99         PsmlManagerService importerService = null;
100
101         try
102         {
103             exporterService = (PsmlManagerService)TurbineServices.getInstance().getService("PsmlImportManager");
104         }
105         catch (org.apache.turbine.services.InstantiationException e)
106         {
107             String JavaDoc msg = "PSML Importer: error loading Psml Exporter Service";
108             logger.error(msg, e);
109             System.out.println(msg);
110             e.printStackTrace();
111             System.exit(0);
112         }
113
114         //
115
// get a handle to the importer service
116
//
117
try
118         {
119             importerService = PsmlManager.getService();
120         }
121         catch (org.apache.turbine.services.InstantiationException e)
122         {
123             String JavaDoc msg = "PSML Importer: error loading Psml Importer Service";
124             logger.error(msg, e);
125             System.out.println(msg);
126             e.printStackTrace();
127             System.exit(0);
128         }
129
130         if (exporterService.getClass().getName().equals(importerService.getClass().getName()))
131         {
132             String JavaDoc msg = "PSML Importer Error: Importer Class cannot equal Exporter Class.";
133             logger.error(msg);
134             System.out.println(msg);
135             System.exit(0);
136         }
137
138         PsmlImporter importer = new PsmlImporter();
139         importer.setCheck(checkImport);
140         boolean ran = importer.run(exporterService, importerService);
141
142         if (ran)
143         {
144             System.out.println("**** PSML Importer - completed");
145         }
146
147         System.exit(1);
148
149     }
150
151     public boolean run(PsmlManagerService exporterService,
152                     PsmlManagerService importerService)
153     {
154         String JavaDoc msg;
155         int count = 0;
156         try
157         {
158            if (check && alreadyImported())
159                 return false;
160
161             msg = "Running with Importer Service: " + importerService.getClass();
162             System.out.println(msg);
163             logger.info(msg);
164
165             msg = "Running with Exporter Service: " + exporterService.getClass();
166             System.out.println(msg);
167             logger.info(msg);
168
169
170             QueryLocator locator = new QueryLocator(QueryLocator.QUERY_ALL);
171             count = exporterService.export(importerService, locator);
172         }
173         catch (Exception JavaDoc e)
174         {
175             System.out.println("Error importing: " + e.toString());
176             logger.error("Error importing: " , e);
177             e.printStackTrace();
178             return false;
179         }
180         msg = "PSMLImporter completed. Exported " + count + " profiles";
181         System.out.println(msg);
182         logger.info(msg);
183         return true;
184     }
185
186
187     /*
188      * Check to see if import has already completed.
189      * Only considers a "onetime" import, checking for the "admin" user.
190      *
191      * @return true if import was already ran.
192      */

193     public boolean alreadyImported()
194     {
195         try
196         {
197             JetspeedUser user = JetspeedSecurity.getUser("admin");
198             QueryLocator ql = new QueryLocator(QueryLocator.QUERY_USER);
199             ql.setUser(user);
200             Iterator JavaDoc iterator = PsmlManager.query(ql);
201             if (iterator.hasNext())
202             {
203                 String JavaDoc msg = "PSMLImporter: Detected database is populated. No need to import.";
204                 System.out.println(msg);
205                 logger.info(msg);
206                 return true; // record found
207
}
208             return false; // record not found
209
}
210         catch (UnknownUserException e)
211         {
212             return false; // record not found
213
}
214         catch (JetspeedSecurityException e)
215         {
216             String JavaDoc msg = "Failed to run import: Database Access Error detecting database on import: ";
217             logger.error(msg, e);
218             System.out.println(msg + e.toString());
219             return true;
220         }
221     }
222
223     public void setCheck(boolean check)
224     {
225         this.check = check;
226     }
227
228     public boolean getCheck()
229     {
230         return this.check;
231     }
232
233 }
234
Popular Tags