1 | |
package fr.paris.lutece.plugins.releaser.util; |
2 | |
|
3 | |
import java.io.BufferedReader; |
4 | |
import java.io.File; |
5 | |
import java.io.FileReader; |
6 | |
import java.io.FileWriter; |
7 | |
import java.io.FilenameFilter; |
8 | |
import java.io.IOException; |
9 | |
import java.io.StringWriter; |
10 | |
import java.util.regex.Matcher; |
11 | |
import java.util.regex.Pattern; |
12 | |
|
13 | |
import javax.xml.parsers.DocumentBuilder; |
14 | |
import javax.xml.parsers.DocumentBuilderFactory; |
15 | |
import javax.xml.parsers.ParserConfigurationException; |
16 | |
import javax.xml.transform.Transformer; |
17 | |
import javax.xml.transform.TransformerException; |
18 | |
import javax.xml.transform.TransformerFactory; |
19 | |
import javax.xml.transform.dom.DOMSource; |
20 | |
import javax.xml.transform.stream.StreamResult; |
21 | |
|
22 | |
import org.eclipse.jgit.api.errors.NoFilepatternException; |
23 | |
import org.w3c.dom.Document; |
24 | |
import org.w3c.dom.Node; |
25 | |
import org.w3c.dom.NodeList; |
26 | |
import org.xml.sax.SAXException; |
27 | |
|
28 | 0 | public class PluginUtils |
29 | |
{ |
30 | |
|
31 | |
public static final String CORE_PLUGIN_NAME1 = "core"; |
32 | |
public static final String CORE_PLUGIN_NAME2 = "lutece-core"; |
33 | |
|
34 | |
private static final String SUFFIX_CONTEXT_FILE = ".xml"; |
35 | |
private static final String CONSTANTE_PLUGIN_PATH = "webapp/WEB-INF/plugins/"; |
36 | |
private static final String CONSTANTE_CORE_CONF = "webapp/WEB-INF/conf/"; |
37 | |
private static final String CONSTANTE_CORE_APP_INFO = "src/java/fr/paris/lutece/portal/service/init/AppInfo.java"; |
38 | |
private static final String REGEXP_VERSION_APP_INFO = "(.*)private static final String APP_VERSION = \"(.*)\";"; |
39 | 0 | private static final Pattern PATTERN_VERSION_APP_INFO = Pattern.compile( REGEXP_VERSION_APP_INFO ); |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
public static boolean isCore( String strPluginName ) |
48 | |
{ |
49 | 0 | return CORE_PLUGIN_NAME1.equals( strPluginName ) || CORE_PLUGIN_NAME2.equals( strPluginName ); |
50 | |
} |
51 | |
|
52 | |
public static String getCoreXMLFile( String strComponentPath) |
53 | |
{ |
54 | 0 | String strCodeXML = strComponentPath+ ( strComponentPath.endsWith( File.separator ) ? "" : File.separator ) + CONSTANTE_CORE_CONF + "core.xml"; |
55 | 0 | File fileCoreXML = new File( strCodeXML ); |
56 | |
|
57 | 0 | if ( !fileCoreXML.exists( ) ) |
58 | |
{ |
59 | 0 | return ""; |
60 | |
} |
61 | |
|
62 | 0 | return strCodeXML; |
63 | |
} |
64 | |
|
65 | |
public static String [ ] getPluginXMLFile( String strComponentPath) |
66 | |
{ |
67 | 0 | String strDirConfPlugins = strComponentPath+ ( strComponentPath.endsWith( File.separator ) ? "" : File.separator ) + CONSTANTE_PLUGIN_PATH; |
68 | 0 | File dirConfPlugins = new File( strDirConfPlugins ); |
69 | |
|
70 | 0 | if ( !dirConfPlugins.exists( ) ) |
71 | |
{ |
72 | 0 | return new String [ 0]; |
73 | |
} |
74 | |
|
75 | 0 | FilenameFilter filterContext = new ContextFileFilter( ); |
76 | 0 | String [ ] filesName = dirConfPlugins.list( filterContext ); |
77 | 0 | for ( int nIndex = 0; nIndex < filesName.length; nIndex++ ) |
78 | |
{ |
79 | 0 | if ( !filesName [nIndex].startsWith( strDirConfPlugins ) ) |
80 | |
{ |
81 | 0 | filesName [nIndex] = strDirConfPlugins + filesName [nIndex]; |
82 | |
} |
83 | |
} |
84 | |
|
85 | 0 | return filesName; |
86 | |
} |
87 | |
|
88 | |
public static String getAppInfoFile( String strBasePath, String strPluginName ) |
89 | |
{ |
90 | 0 | String strAppInfo = strBasePath + ( strBasePath.endsWith( File.separator ) ? "" : File.separator ) + strPluginName |
91 | 0 | + ( strPluginName.endsWith( File.separator ) ? "" : File.separator ) + File.separator + CONSTANTE_CORE_APP_INFO; |
92 | 0 | File fileAppInfo = new File( strAppInfo ); |
93 | 0 | if ( !fileAppInfo.exists( ) ) |
94 | |
{ |
95 | 0 | return ""; |
96 | |
} |
97 | |
|
98 | 0 | return strAppInfo; |
99 | |
} |
100 | |
|
101 | |
public static boolean updateAppInfoFile( String strFile, String strVersion, CommandResult commandResult ) |
102 | |
{ |
103 | 0 | boolean bReplace = false; |
104 | 0 | BufferedReader br = null; |
105 | 0 | StringBuilder sbNewFileContent = null; |
106 | |
try |
107 | |
{ |
108 | 0 | sbNewFileContent = new StringBuilder( ); |
109 | 0 | br = new BufferedReader( new FileReader( strFile ) ); |
110 | 0 | while ( br.ready( ) ) |
111 | |
{ |
112 | 0 | String strNewLine = br.readLine( ); |
113 | 0 | Matcher matcher = PATTERN_VERSION_APP_INFO.matcher( strNewLine ); |
114 | 0 | if ( matcher.matches( ) ) |
115 | |
{ |
116 | 0 | if ( matcher.groupCount( ) >= 1 ) |
117 | |
{ |
118 | 0 | String strCurrentVersion = matcher.group( 2 ); |
119 | 0 | if ( !strCurrentVersion.equals( strVersion ) ) |
120 | |
{ |
121 | 0 | commandResult.getLog( ).append( "Updating core version from " + strCurrentVersion + " to " + strVersion ); |
122 | 0 | strNewLine = strNewLine.replace( strCurrentVersion, strVersion ); |
123 | 0 | bReplace = true; |
124 | |
} |
125 | |
else |
126 | |
{ |
127 | 0 | commandResult.getLog( ).append( "Version is already " + strVersion ); |
128 | |
} |
129 | |
} |
130 | |
} |
131 | |
|
132 | 0 | sbNewFileContent.append( strNewLine + "\n" ); |
133 | 0 | } |
134 | |
|
135 | |
} |
136 | 0 | catch( Exception ex ) |
137 | |
{ |
138 | 0 | ReleaserUtils.addTechnicalError( commandResult, ex.getMessage( ), ex ); |
139 | |
} |
140 | |
finally |
141 | |
{ |
142 | 0 | if ( br != null ) |
143 | |
{ |
144 | |
try |
145 | |
{ |
146 | 0 | br.close( ); |
147 | |
} |
148 | 0 | catch( IOException e1 ) |
149 | |
{ |
150 | 0 | ReleaserUtils.addTechnicalError( commandResult, e1.getMessage( ), e1 ); |
151 | 0 | } |
152 | |
} |
153 | |
} |
154 | |
|
155 | 0 | if ( !bReplace ) |
156 | |
{ |
157 | 0 | return false; |
158 | |
} |
159 | |
|
160 | 0 | FileWriter fw = null; |
161 | |
try |
162 | |
{ |
163 | 0 | fw = new FileWriter( strFile ); |
164 | 0 | fw.append( sbNewFileContent.toString( ) ); |
165 | |
} |
166 | 0 | catch( Exception ex ) |
167 | |
{ |
168 | 0 | ReleaserUtils.addTechnicalError( commandResult, ex.getMessage( ), ex ); |
169 | |
} |
170 | |
finally |
171 | |
{ |
172 | 0 | if ( fw != null ) |
173 | |
{ |
174 | |
try |
175 | |
{ |
176 | 0 | fw.close( ); |
177 | |
} |
178 | 0 | catch( IOException ex ) |
179 | |
{ |
180 | 0 | ReleaserUtils.addTechnicalError( commandResult, ex.getMessage( ), ex ); |
181 | 0 | } |
182 | |
} |
183 | |
} |
184 | 0 | return false; |
185 | |
} |
186 | |
|
187 | |
public static String updatePluginXMLVersion( String strFile, String strNewVersion, CommandResult commandResult) |
188 | |
{ |
189 | 0 | FileWriter fw = null; |
190 | 0 | boolean bFileClosed = false; |
191 | |
try |
192 | |
{ |
193 | 0 | DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance( ); |
194 | 0 | DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder( ); |
195 | 0 | Document doc = docBuilder.parse( strFile ); |
196 | |
|
197 | 0 | doc.getDocumentElement( ).normalize( ); |
198 | |
|
199 | 0 | NodeList versionList = doc.getElementsByTagName( "version" ); |
200 | 0 | Node version = versionList.item( 0 ); |
201 | |
|
202 | 0 | version.getFirstChild( ).setNodeValue( strNewVersion ); |
203 | |
|
204 | 0 | DOMSource domSource = new DOMSource( doc ); |
205 | |
|
206 | 0 | StringWriter sw = new StringWriter( ); |
207 | 0 | StreamResult result = new StreamResult( sw ); |
208 | |
|
209 | 0 | TransformerFactory tf = TransformerFactory.newInstance( ); |
210 | 0 | Transformer transformer = tf.newTransformer( ); |
211 | 0 | transformer.transform( domSource, result ); |
212 | |
|
213 | 0 | fw = new FileWriter( new File( strFile ) ); |
214 | |
|
215 | 0 | fw.append( sw.toString( ) ); |
216 | |
|
217 | 0 | fw.flush( ); |
218 | |
|
219 | 0 | fw.close( ); |
220 | |
|
221 | 0 | bFileClosed = true; |
222 | |
|
223 | |
} |
224 | 0 | catch(Exception ex) |
225 | |
{ |
226 | 0 | ReleaserUtils.addTechnicalError( commandResult, ex.getMessage( ), ex ); |
227 | |
} |
228 | |
|
229 | |
finally |
230 | |
{ |
231 | 0 | if ( fw != null && !bFileClosed ) |
232 | |
{ |
233 | |
try |
234 | |
{ |
235 | 0 | fw.close( ); |
236 | |
} |
237 | 0 | catch( IOException e ) |
238 | |
{ |
239 | 0 | e.printStackTrace( ); |
240 | 0 | } |
241 | |
} |
242 | |
} |
243 | |
|
244 | 0 | return ""; |
245 | |
} |
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | 0 | static class ContextFileFilter implements FilenameFilter |
251 | |
{ |
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
public boolean accept(File file, String strName) |
259 | |
{ |
260 | 0 | return strName.endsWith( SUFFIX_CONTEXT_FILE ); |
261 | |
} |
262 | |
} |
263 | |
|
264 | |
} |