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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
public class SWESessionFactory extends AnnotationSessionFactoryBean{
private static final Log log = LogFactory.getLog(SWESessionFactory.class);
/*Le nom du fichier de properties contenant les classes*/
private String annotatedClassesPropertiesFile="annotatedClasses.properties";
/*Nom du fichier properties pour la connexion en base de données*/
private String dataBasePropertiesFile="databaseProperties.properties";
/*Nom du fichier properties pour les propriétés d'hibernate*/
private String hibernatePropertiesFile="hibernateProperties.properties";
/**
*
*/
public SWESessionFactory() {
// TODO Auto-generated constructor stub
super();
}
/**
*
* @param byProperties : si vrai alors la configuration doit être faite par
* fichiers properties, faux on utilisation l'annotation SWEFactory
* @return une instance de la session Factory
*/
public final SessionFactory configure(boolean byProperties){
if(byProperties)return propertiesConfiguration();
return annotatedConfiguration();
}
/**
*
* @return une instance de la session factory prêt à l'emploi
* avec une configuration avec des fichiers properties
*/
@SuppressWarnings("rawtypes")
private final SessionFactory propertiesConfiguration(){
log.debug("Initialisation de la sessionFactory par les fichiers de Properties");
List<Class> classes = null;
Class[] annotatedClasses;
classes=loadClasses();
annotatedClasses=classes.toArray(new Class[classes.size()]);
setAnnotatedClasses(annotatedClasses);
setHibernateProperties(loadPropertiesFile(hibernatePropertiesFile));
setDataSource(loadDataSource(null));
try {
log.debug("Chargement de la session Factory");
return buildSessionFactory();
} catch (Exception e) {
// TODO Auto-generated catch block
log.error("Le chargement de la session Factory a échoué");
throw new SWEException("Impossible de localiser la SessionFactory",e);
}
}
/**
*
* @return une instance de la session factory prêt à l'emploi
* Dans ce cas on impose à la classe Dao d'être annotée
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private final SessionFactory annotatedConfiguration(){
log.debug("Initialisation de la session Factory avec les annotations");
StackTraceElement stes[]=Thread.currentThread().getStackTrace();
SWEFactory swef=null;
for(StackTraceElement ste:stes){
String className=ste.getClassName();
Class c=null;
try {
log.debug("Détermination de la classe courrente");
c=getClass().getClassLoader().loadClass(className);
} catch (ClassNotFoundException e) {
log.debug("On ne passe pas ici");
e.printStackTrace();
}
if(c.isAnnotationPresent(SWEFactory.class)){
swef=(SWEFactory) c.getAnnotation(SWEFactory.class);
log.debug("La classe courrente a été trouvée il déclare bien l'annotation SWEFactory :"+swef);
break;
}
}
setAnnotatedClasses(swef.annotatedClasses());
Properties p=new Properties();
PropertyDataSource pdss[]=swef.dataSource();
for(PropertyDataSource pds:pdss)p.setProperty(pds.key().name(), pds.value());
setDataSource(loadDataSource(p));
p.clear();
HibernateProperties hps[]=swef.hibernateProperties();
for(HibernateProperties hp:hps) p.setProperty(hp.key(), hp.value());
setHibernateProperties(p);
try {
log.debug("Chargement de la session Factory");
return buildSessionFactory();
} catch (Exception e) {
log.error("Le chargement de la session Factory a échoué");
throw new SWEException("Impossible de localiser la SessionFactory",e);
}
}
/**
*
* @param annotatedClassesPropertiesFile: préciser le nom du fichier properties pour les classes
*/
public void setAnnotatedPropertiesFile(String annotatedClassesPropertiesFile){
this.annotatedClassesPropertiesFile=annotatedClassesPropertiesFile;
}
/**
*
* @param dataBasePropertiesFile: préciser le nom du fichier properties pour la dataBaseProperties
*/
public void setDataBasePropertiesFile(String dataBasePropertiesFile){
this.dataBasePropertiesFile=dataBasePropertiesFile;
}
/**
*
* @param hibernatePropertiesFile: préciser le nom du fichier properties hibernate
*/
public void setHibernatePropertiesFile(String hibernatePropertiesFile){
this.hibernatePropertiesFile=hibernatePropertiesFile;
}
/**
*
* @return la liste des classes annotées dans le fichier properties
*/
@SuppressWarnings("rawtypes")
private final List<Class> loadClasses(){
List<Class> classes=new ArrayList<Class>();
Properties p=null;
log.debug("Localisons du fichier de properties "+annotatedClassesPropertiesFile);
p = loadPropertiesFile(annotatedClassesPropertiesFile);
Set<Entry<Object,Object>> set=p.entrySet();
Iterator<Entry<Object,Object>> it=set.iterator();
while(it.hasNext()){
Entry<Object,Object> en=it.next();
Class<?> c;
String cls=en.getValue().toString();
try {
log.debug("Mise à jour de la classe "+cls);
c = Class.forName(cls);
} catch (ClassNotFoundException e) {
log.error("La classe "+cls+" n'existe pas");
throw new SWEException("La classe "+cls+" n'existe pas",e);
}
classes.add(c);
}
return classes;
}
/**
* @param p: contient les propriétés de la base de données qui peut provenir soit
* du fichier properties sot de l'annotation
* @return l'instance de la DataSource qui nous permettra d'établir une connexion
* à la base de données
*/
private final DriverManagerDataSource loadDataSource(Properties p){
DriverManagerDataSource dataSource=new DriverManagerDataSource();
p=p==null?loadPropertiesFile(dataBasePropertiesFile):p;
dataSource.setDriverClassName(p.getProperty("driverClassName"));
dataSource.setPassword(p.getProperty("password"));
dataSource.setUrl(p.getProperty("url"));
dataSource.setUsername(p.getProperty("username"));
dataSource.setConnectionProperties(p);
return dataSource;
}
private final Properties loadPropertiesFile(String name){
InputStream in=getClass().getClassLoader().getResourceAsStream(name);
Properties p=new Properties();
try {
p.load(in);
} catch (IOException e) {
e.printStackTrace();
}
return p;
}
} |
Partager