1 16 17 package org.apache.jetspeed.services.psmlmanager; 18 19 import java.util.Iterator ; 20 21 import org.apache.turbine.services.TurbineServices; 22 23 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 import org.apache.jetspeed.services.PsmlManager; 33 import org.apache.jetspeed.om.profile.QueryLocator; 34 35 import org.apache.turbine.util.TurbineConfig; 36 37 43 public class PsmlImporter 44 { 45 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 args[]) 57 { 58 System.out.println("***** PSML Importer *****"); 59 boolean checkImport = true; 60 61 try 65 { 66 String root = "./webapp"; 67 String 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 e) 87 { 88 String 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 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 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 try 118 { 119 importerService = PsmlManager.getService(); 120 } 121 catch (org.apache.turbine.services.InstantiationException e) 122 { 123 String 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 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 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 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 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 iterator = PsmlManager.query(ql); 201 if (iterator.hasNext()) 202 { 203 String msg = "PSMLImporter: Detected database is populated. No need to import."; 204 System.out.println(msg); 205 logger.info(msg); 206 return true; } 208 return false; } 210 catch (UnknownUserException e) 211 { 212 return false; } 214 catch (JetspeedSecurityException e) 215 { 216 String 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 |