1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package net.sf.jguard.ext;
29
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.FileNotFoundException;
33 import java.io.IOException;
34 import java.net.URI;
35 import java.net.URISyntaxException;
36 import java.util.Map;
37 import java.util.Properties;
38
39 import java.util.logging.Level;
40 import net.sf.jguard.core.util.FileUtils;
41 import net.sf.jguard.ext.database.ConnectionFactory;
42 import net.sf.jguard.core.util.XMLUtils;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46
47
48
49
50 public class JdbcManagerHelper {
51
52 public static final String DB_PROPERTIES_LOCATION = "dbPropertiesLocation";
53 public static final String CREATE_REQUIRED_DATABASE_ENTITIES = "createRequiredDatabaseEntities";
54 public static final String IMPORT_XML_DATA_KEY = "importXmlDataKey";
55 public static final String IMP0RT_XML_DATA_VALUE = "importXmlDataValue";
56 public static final String XML_FILE_NAME = "xmlFileName";
57
58 private static final Logger logger = LoggerFactory.getLogger(JdbcManagerHelper.class.getName());
59
60
61
62
63
64
65
66
67
68 public static void jdbcInit(JdbcManager jdbcManager,ConnectionFactory connectionFactory,Properties props,Map options) {
69
70 String dbPropertiesLocation = (String)options.get(DB_PROPERTIES_LOCATION);
71 String createRequiredDatabaseEntititesStr = (String)options.get(CREATE_REQUIRED_DATABASE_ENTITIES);
72 boolean createRequiredDatabaseEntities;
73 if(createRequiredDatabaseEntititesStr==null || "".equals(createRequiredDatabaseEntititesStr)){
74 createRequiredDatabaseEntities = true;
75 }else{
76 createRequiredDatabaseEntities = Boolean.valueOf((String)options.get(CREATE_REQUIRED_DATABASE_ENTITIES)).booleanValue();
77 }
78 String importXmlDataKey = (String)options.get(IMPORT_XML_DATA_KEY);
79 boolean importXmlDataValue = Boolean.valueOf((String)options.get(IMP0RT_XML_DATA_VALUE)).booleanValue();
80 if(dbPropertiesLocation!=null && !"".equals(dbPropertiesLocation)){
81 dbPropertiesLocation = XMLUtils.resolveLocation(dbPropertiesLocation);
82 }else{
83 throw new IllegalArgumentException(" dbPropertiesLocation is null or empty ");
84 }
85 try {
86 File file = FileUtils.getFile(new URI(dbPropertiesLocation));
87 props.load(new FileInputStream(file));
88 logger.debug(" JdbcManager properties = "+props);
89 } catch (FileNotFoundException e2) {
90 logger.error(" database properties file is not found at this location "+dbPropertiesLocation);
91 e2.printStackTrace();
92 } catch (IOException e2) {
93 logger.error(" database properties file is not accesible this location "+dbPropertiesLocation+"\n "+e2.getMessage());
94 } catch (URISyntaxException e) {
95 logger.error( " uri of the database properties file hasn't got a valid synthax ",e);
96 }
97
98 if(createRequiredDatabaseEntities){
99 jdbcManager.createRequiredDatabaseEntities(props,connectionFactory);
100 }
101 boolean empty = jdbcManager.isEmpty();
102 String XMlFileName = (String)options.get(XML_FILE_NAME);
103 String xmlFileLocation = dbPropertiesLocation.substring(0,dbPropertiesLocation.lastIndexOf('/'))+"/"+XMlFileName;
104 if(empty && importXmlDataValue){
105 logger.info(" importing XML data into your database from the XML located here:"+xmlFileLocation);
106 jdbcManager.insertRequiredData(xmlFileLocation);
107 }else if(empty && !importXmlDataValue){
108 logger.warn(" database entities required by jGuard are empty. to fill them with an XML located here :"+xmlFileLocation+"\n you have to set the JdbcManager (JdbcAuthenticationManager or JdbcAuthorizationManager) option "+(String)importXmlDataKey+" to 'true' ");
109 }
110 }
111 }