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 package fr.paris.lutece.plugins.lutecetools.service;
40
41 import fr.paris.lutece.plugins.lutecetools.business.Component;
42 import fr.paris.lutece.portal.service.util.AppLogService;
43 import fr.paris.lutece.util.httpaccess.HttpAccess;
44 import fr.paris.lutece.util.httpaccess.HttpAccessException;
45 import java.io.ByteArrayInputStream;
46 import java.io.IOException;
47 import java.io.InputStream;
48 import java.util.ArrayList;
49 import java.util.HashMap;
50 import java.util.List;
51 import javax.xml.parsers.DocumentBuilder;
52 import javax.xml.parsers.DocumentBuilderFactory;
53 import javax.xml.parsers.ParserConfigurationException;
54 import org.w3c.dom.DOMException;
55 import org.w3c.dom.Document;
56 import org.w3c.dom.Element;
57 import org.w3c.dom.NodeList;
58 import org.xml.sax.InputSource;
59 import org.xml.sax.SAXException;
60
61
62
63
64
65 public final class PomService
66 {
67
68 private static final String TAG_DEPENDENCY = "dependency";
69 private static final String TAG_DEPENDENCY_GROUPID = "groupId";
70 private static final String TAG_DEPENDENCY_ARTIFACT_ID = "artifactId";
71 private static final String TAG_DEPENDENCY_VERSION = "version";
72 private static final String TAG_DEPENDENCY_TYPE = "type";
73
74 private static final String CONSTANT_LUTECE_GROUP = "fr.paris.lutece";
75
76 private static final String EXCEPTION_MESSAGE = "LuteceTools - PomService : Error retrieving Pom tags : ";
77
78 private static PomService _singleton;
79
80
81
82
83 private PomService( )
84 {
85 }
86
87
88
89
90
91
92 public static synchronized PomService instance( )
93 {
94 if ( _singleton == null )
95 {
96 _singleton = new PomService( );
97 }
98
99 return _singleton;
100 }
101
102
103
104
105
106
107
108
109 public void getLuteceDependencies( Component component, String strPomUrl, boolean bSnapshot, StringBuilder sbLogs )
110 {
111 try
112 {
113 HttpAccess httpAccess = new HttpAccess( );
114 String strPom = httpAccess.doGet( strPomUrl );
115
116 DocumentBuilderFactory documentBuilder = DocumentBuilderFactory.newInstance( );
117 InputStream is = new ByteArrayInputStream( strPom.getBytes( ) );
118
119
120 DocumentBuilder builder = documentBuilder.newDocumentBuilder( );
121 Document document = builder.parse( new InputSource( is ) );
122 Element root = document.getDocumentElement( );
123
124 List<HashMap<String, String>> dependencyList = new ArrayList<>( );
125
126 NodeList nodeDependencyList = root.getElementsByTagName( TAG_DEPENDENCY );
127 for ( int i = 0; i < nodeDependencyList.getLength( ); i++ )
128 {
129 Element elmt = (Element) nodeDependencyList.item( i );
130 NodeList groupList = elmt.getElementsByTagName( TAG_DEPENDENCY_GROUPID );
131 String strGroupId = null;
132 if ( groupList != null && groupList.getLength( ) > 0 )
133 {
134 Element groupElement = (Element) groupList.item( 0 );
135 strGroupId = groupElement.getTextContent( );
136 }
137 if ( strGroupId == null || !strGroupId.startsWith( CONSTANT_LUTECE_GROUP ) )
138 {
139 continue;
140 }
141 NodeList artifactIdList = elmt.getElementsByTagName( TAG_DEPENDENCY_ARTIFACT_ID );
142 String strArtifactId = null;
143 if ( artifactIdList != null && artifactIdList.getLength( ) > 0 )
144 {
145 Element artifactElement = (Element) artifactIdList.item( 0 );
146 strArtifactId = artifactElement.getTextContent( );
147 }
148
149 NodeList versionList = elmt.getElementsByTagName( TAG_DEPENDENCY_VERSION );
150 String strVersion = null;
151 if ( versionList != null && versionList.getLength( ) > 0 )
152 {
153 Element versionElement = (Element) versionList.item( 0 );
154 strVersion = versionElement.getTextContent( );
155 }
156
157 NodeList typeList = elmt.getElementsByTagName( TAG_DEPENDENCY_TYPE );
158 String strType = null;
159 if ( typeList != null && typeList.getLength( ) > 0 )
160 {
161 Element typeElement = (Element) typeList.item( 0 );
162 strType = typeElement.getTextContent( );
163 }
164
165 HashMap<String, String> dependencyMap = new HashMap<>( );
166 dependencyMap.put( TAG_DEPENDENCY_GROUPID, strGroupId );
167 dependencyMap.put( TAG_DEPENDENCY_ARTIFACT_ID, strArtifactId );
168 dependencyMap.put( TAG_DEPENDENCY_VERSION, strVersion );
169 dependencyMap.put( TAG_DEPENDENCY_TYPE, strType );
170
171 dependencyList.add( dependencyMap );
172 }
173
174 component.set( ( bSnapshot ? "SNAPSHOT_" : "" ) + Component.DEPENDENCY_LIST, dependencyList );
175
176 }
177 catch ( IOException | ParserConfigurationException | DOMException | SAXException e )
178 {
179 AppLogService.error( EXCEPTION_MESSAGE + component.getArtifactId( ) + " : " + e.getMessage( ), e );
180 }
181 catch ( HttpAccessException e )
182 {
183 sbLogs.append( "\n*** ERROR *** Error reading pom for component :" ).append( component.getArtifactId( ) )
184 .append( " [url : " ).append( strPomUrl ).append( "]" ).append( EXCEPTION_MESSAGE );
185 }
186 }
187
188 }