24
24
25
25
package org .silverpeas .admin .domain ;
26
26
27
- import java .io .File ;
28
- import java .io .FileWriter ;
29
- import java .io .IOException ;
30
- import java .io .PrintWriter ;
31
-
32
- import javax .annotation .PostConstruct ;
33
- import javax .inject .Inject ;
34
- import javax .inject .Named ;
35
-
36
- import org .silverpeas .admin .domain .exception .DomainAuthenticationPropertiesAlreadyExistsException ;
37
- import org .silverpeas .admin .domain .exception .DomainConflictException ;
38
- import org .silverpeas .admin .domain .exception .DomainCreationException ;
39
- import org .silverpeas .admin .domain .exception .DomainDeletionException ;
40
- import org .silverpeas .admin .domain .exception .DomainPropertiesAlreadyExistsException ;
41
- import org .silverpeas .admin .domain .repository .SQLDomainRepository ;
42
-
43
27
import com .silverpeas .util .StringUtil ;
44
28
import com .silverpeas .util .template .SilverpeasTemplate ;
45
29
import com .silverpeas .util .template .SilverpeasTemplateFactory ;
30
+ import com .stratelia .silverpeas .domains .sqldriver .SQLSettings ;
46
31
import com .stratelia .silverpeas .silvertrace .SilverTrace ;
47
32
import com .stratelia .webactiv .beans .admin .AdminException ;
48
33
import com .stratelia .webactiv .beans .admin .Domain ;
49
34
import com .stratelia .webactiv .util .FileRepositoryManager ;
50
35
import com .stratelia .webactiv .util .FileServerUtils ;
51
36
import com .stratelia .webactiv .util .ResourceLocator ;
37
+ import org .silverpeas .admin .domain .exception .DomainAuthenticationPropertiesAlreadyExistsException ;
38
+ import org .silverpeas .admin .domain .exception .DomainConflictException ;
39
+ import org .silverpeas .admin .domain .exception .DomainCreationException ;
40
+ import org .silverpeas .admin .domain .exception .DomainDeletionException ;
41
+ import org .silverpeas .admin .domain .exception .DomainPropertiesAlreadyExistsException ;
42
+ import org .silverpeas .admin .domain .repository .SQLDomainRepository ;
43
+
44
+ import javax .annotation .PostConstruct ;
45
+ import javax .inject .Inject ;
46
+ import javax .inject .Named ;
47
+ import java .io .File ;
48
+ import java .io .FileWriter ;
49
+ import java .io .IOException ;
50
+ import java .io .PrintWriter ;
51
+ import java .text .Normalizer ;
52
52
53
53
@ Named ("sqlDomainService" )
54
54
public class SQLDomainService extends AbstractDomainService {
@@ -66,7 +66,9 @@ void init() {
66
66
adminSettings = new ResourceLocator ("org.silverpeas.beans.admin.admin" , "" );
67
67
}
68
68
69
- private void checkFileName (String fileDomainName ) throws DomainAuthenticationPropertiesAlreadyExistsException , DomainPropertiesAlreadyExistsException {
69
+ private void checkFileName (String fileDomainName )
70
+ throws DomainAuthenticationPropertiesAlreadyExistsException ,
71
+ DomainPropertiesAlreadyExistsException {
70
72
// Check properties files availability
71
73
// org.silverpeas.domains.domain<domainName>.properties
72
74
// org.silverpeas.authentication.autDomain<domainName>.properties
@@ -83,23 +85,25 @@ private void checkFileName(String fileDomainName) throws DomainAuthenticationPro
83
85
}
84
86
}
85
87
86
- //transformation du nom du domaine, nécessaire pour pouvoir créer les fichiers sur le fileSystem
87
- //et créer les tables dans la BD
88
+ /**
89
+ * Gets a file name without special characters and without accentued characters in the aim to
90
+ * create domain property files safely on file system.
91
+ * @param domainName domain name with maybe some special characters and/or accentued characters
92
+ * @return
93
+ */
88
94
private String getCorrectDomainFileName (String domainName ) {
89
- //remplace les caractères accentués non compatibles avec les fichiers fileSystem et les noms de tables BD par les caractères non accentués correspondants
95
+
96
+ // Normalizing (accents, puissance, ...)
90
97
String fileDomainName = FileServerUtils .replaceAccentChars (domainName );
98
+ fileDomainName = Normalizer .normalize (fileDomainName , Normalizer .Form .NFKD );
91
99
92
- //remplace les caractères spéciaux et les espaces non compatibles avec les fichiers fileSystem et les noms de tables BD par caractère '_'
93
- fileDomainName = fileDomainName .replaceAll ("[^A-Za-z0-9] " , "_" );
100
+ // Replacing of each sequence of special characters by one underscore
101
+ fileDomainName = fileDomainName .replaceAll ("[^\\ p{Alnum}]+ " , "_" );
94
102
95
- //tronque le nom à 42 caractères pour être compatible avec les noms de tables BD
96
- if (fileDomainName .length ()>42 ) {
97
- fileDomainName = fileDomainName .substring (0 , 42 );
103
+ // Limitations of some databases on length of table or column names
104
+ return StringUtil .left (fileDomainName , SQLSettings .DATABASE_TABLE_NAME_MAX_LENGTH );
98
105
}
99
106
100
- return fileDomainName ;
101
- }
102
-
103
107
@ Override
104
108
public String createDomain (Domain domainToCreate ) throws DomainConflictException ,
105
109
DomainCreationException {
@@ -168,9 +172,16 @@ public String createDomain(Domain domainToCreate) throws DomainConflictException
168
172
@ Override
169
173
public String deleteDomain (Domain domainToRemove ) throws DomainDeletionException {
170
174
171
- //set nouveau nom pour le fileSystem et la BD
172
- String domainPropertiesPath = domainToRemove .getPropFileName ();
173
- String fileDomainName = domainPropertiesPath .substring (29 ); //supprime org.silverpeas.domains.domain
175
+ // Retrieve the prefix of a domain property file name
176
+ String separator = "#@#@#@#@#" ;
177
+ String domainPropertyPrefix = new File (
178
+ FileRepositoryManager .getDomainPropertiesPath (separator ).replaceAll (separator + ".*$" , "" ))
179
+ .getName ();
180
+ // Get the domain property file name without the package
181
+ String domainPropertyFileName =
182
+ domainToRemove .getPropFileName ().replaceAll ("[\\ p{Alnum}]+\\ .+" , "" );
183
+ // Compute the common property file name by removing the prefix of a domain property file name
184
+ String fileDomainName = domainPropertyFileName .replaceFirst (domainPropertyPrefix , "" );
174
185
domainToRemove .setName (fileDomainName );
175
186
176
187
// unregister new Domain dans st_domain
@@ -210,10 +221,7 @@ private void removePropertiesFiles(String domainName) {
210
221
* @throws DomainCreationException
211
222
*/
212
223
private void generateDomainPropertiesFile (Domain domainToCreate ) throws DomainCreationException {
213
- SilverTrace
214
- .info (
215
- "admin" ,
216
- "SQLDomainService.generateDomainPropertiesFile()" ,
224
+ SilverTrace .info ("admin" , "SQLDomainService.generateDomainPropertiesFile()" ,
217
225
"root.MSG_GEN_ENTER_METHOD" );
218
226
219
227
String domainName = domainToCreate .getName ();
@@ -252,10 +260,7 @@ private void generateDomainPropertiesFile(Domain domainToCreate) throws DomainCr
252
260
*/
253
261
private void generateDomainAuthenticationPropertiesFile (Domain domainToCreate )
254
262
throws DomainCreationException {
255
- SilverTrace
256
- .info (
257
- "admin" ,
258
- "SQLDomainService.generateDomainAuthenticationPropertiesFile()" ,
263
+ SilverTrace .info ("admin" , "SQLDomainService.generateDomainAuthenticationPropertiesFile()" ,
259
264
"root.MSG_GEN_ENTER_METHOD" );
260
265
261
266
String domainName = domainToCreate .getName ();
@@ -287,20 +292,19 @@ private void generateDomainAuthenticationPropertiesFile(Domain domainToCreate)
287
292
"SQLDomainService.generateDomainAuthenticationPropertiesFile()" , domainToCreate
288
293
.toString (), e );
289
294
} finally {
295
+ if (out != null ) {
290
296
out .close ();
291
297
}
292
298
}
299
+ }
293
300
294
301
/**
295
302
* Remove domain authentication and settings properties file
296
303
* @param domainToRemove domain to remove
297
304
* @throws DomainDeletionException
298
305
*/
299
306
private void removeDomainPropertiesFile (Domain domainToRemove ) {
300
- SilverTrace
301
- .info (
302
- "admin" ,
303
- "SQLDomainService.removeDomainAuthenticationPropertiesFile()" ,
307
+ SilverTrace .info ("admin" , "SQLDomainService.removeDomainAuthenticationPropertiesFile()" ,
304
308
"root.MSG_GEN_ENTER_METHOD" );
305
309
306
310
String domainName = domainToRemove .getName ();
@@ -314,11 +318,8 @@ private void removeDomainPropertiesFile(Domain domainToRemove) {
314
318
boolean domainPropertiesFileDeleted = domainPropertiesFile .delete ();
315
319
boolean authenticationPropertiesFileDeleted = authenticationPropertiesFile .delete ();
316
320
317
- if ((!domainPropertiesFileDeleted ) || (!authenticationPropertiesFileDeleted )) {
318
- SilverTrace
319
- .warn (
320
- "admin" ,
321
- "SQLDomainService.removeDomainAuthenticationPropertiesFile()" ,
321
+ if (!(domainPropertiesFileDeleted && authenticationPropertiesFileDeleted )) {
322
+ SilverTrace .warn ("admin" , "SQLDomainService.removeDomainAuthenticationPropertiesFile()" ,
322
323
"admin.EX_DELETE_DOMAIN_PROPERTIES" , "domainPropertiesFileDeleted:" +
323
324
domainPropertiesFileDeleted + ", authenticationPropertiesFileDeleted:" +
324
325
authenticationPropertiesFileDeleted );
0 commit comments