View Javadoc

1   /*
2   jGuard is a security framework based on top of jaas (java authentication and authorization security).
3   it is written for web applications, to resolve simply, access control problems.
4   version $Name$
5   http://sourceforge.net/projects/jguard/
6   
7   Copyright (C) 2004  Charles GAY
8   
9   This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13  
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  Lesser General Public License for more details.
18  
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  
23  
24  jGuard project home page:
25  http://sourceforge.net/projects/jguard/
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   * Helper class to initialize JdbcManager.
48   * @author <a href="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
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  	 * create required Database entities (tables, constraints) if needed,
62  	 * and import in empty tables from an XML file optionally too.
63  	 * @param jdbcManager
64  	 * @param connectionFactory
65  	 * @param props
66  	 * @param options
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 }