11package com .genexus .util ;
22
33import java .io .File ;
4+ import java .io .IOException ;
45import java .util .Hashtable ;
56
6- import com .genexus .ApplicationContext ;
7+ import com .fasterxml .jackson .core .JsonParseException ;
8+ import com .fasterxml .jackson .databind .JsonMappingException ;
9+ import com .fasterxml .jackson .dataformat .xml .XmlMapper ;
10+ import com .fasterxml .jackson .dataformat .xml .ser .ToXmlGenerator ;
711import com .genexus .ModelContext ;
812import com .genexus .internet .HttpContext ;
13+ import com .genexus .util .cloudservice .Property ;
14+ import com .genexus .util .cloudservice .Service ;
15+ import com .genexus .util .cloudservice .Services ;
916import com .genexus .webpanels .HttpContextWeb ;
10- import com .genexus .xml .XMLReader ;
17+ import com .genexus .diagnostics .core .ILogger ;
18+ import com .genexus .diagnostics .core .LogManager ;
1119
1220public class GXServices {
21+
22+ public static final ILogger logger = LogManager .getLogger (GXServices .class );
23+
1324 private static final boolean DEBUG = com .genexus .DebugFlag .DEBUG ;
14- public static final String WEBNOTIFICATIONS_SERVICE = "WebNotifications" ;
25+ public static final String WEBNOTIFICATIONS_SERVICE = "WebNotifications" ;
1526 public static final String STORAGE_SERVICE = "Storage" ;
1627 public static final String STORAGE_APISERVICE = "StorageAPI" ;
1728 public static final String CACHE_SERVICE = "Cache" ;
@@ -23,7 +34,7 @@ public class GXServices {
2334 public GXServices () {
2435 readServices ("" );
2536 }
26-
37+
2738 public GXServices (String basePath ) {
2839 readServices (basePath );
2940 }
@@ -33,7 +44,7 @@ public static GXServices getInstance() {
3344 instance = new GXServices ();
3445 return instance ;
3546 }
36-
47+
3748 public static GXServices getInstance (String basePath ) {
3849 if (instance == null )
3950 instance = new GXServices (basePath );
@@ -44,29 +55,46 @@ public static void endGXServices() {
4455 instance = null ;
4556 }
4657
47- public static void loadFromFile (String basePath , String fileName , GXServices services ){
58+ public static void loadFromFile (String basePath , String fileName , GXServices services ) {
4859 if (basePath .equals ("" )) {
4960 basePath = services .configBaseDirectory ();
5061 }
5162 String fullPath = basePath + fileName ;
52- XMLReader reader = new XMLReader ();
53- reader .open (fullPath );
54- reader .readType (1 , "Services" );
55- reader .read ();
56- if (reader .getErrCode () == 0 ) {
57- while (!reader .getName ().equals ("Services" )) {
58- services .processService (reader );
59- reader .read ();
60- if (reader .getName ().equals ("Service" ) && reader .getNodeType () == 2 ) //</Service>
61- reader .read ();
62- }
63- reader .close ();
63+ XmlMapper xmlMapper = new XmlMapper ();
64+ xmlMapper .configure (ToXmlGenerator .Feature .WRITE_XML_DECLARATION , true );
65+ Services xmlServices = null ;
66+ try {
67+ xmlServices = xmlMapper .readValue (new File (fullPath ), Services .class );
68+ } catch (JsonParseException e ) {
69+ logger .error (e .getMessage (), e );
70+ e .printStackTrace ();
71+ } catch (JsonMappingException e ) {
72+ logger .error (e .getMessage (), e );
73+ e .printStackTrace ();
74+ } catch (IOException e ) {
75+ logger .error (e .getMessage (), e );
76+ e .printStackTrace ();
6477 }
65- else
66- {
67- if (!ApplicationContext .getInstance ().getReorganization () && DEBUG )
68- {
69- System .out .println ("GXServices - Could not load Services Config: " + fullPath + " - " + reader .getErrDescription ());
78+
79+ if (services == null )
80+ services = new GXServices ();
81+
82+ for (Service xmlService : xmlServices .getService ()) {
83+ GXService service = new GXService ();
84+ service .setName (xmlService .getName ());
85+ service .setType (xmlService .getType ());
86+ service .setClassName (xmlService .getClassName ());
87+ service .setAllowMultiple (xmlService .getAllowMultiple ());
88+ GXProperties ptys = new GXProperties ();
89+ for (Property xmlPty : xmlService .getProperties ().getProperty ()) {
90+ ptys .add (xmlPty .getName (), xmlPty .getValue ());
91+ }
92+ service .setProperties (ptys );
93+
94+ if (service .getAllowMultiple ()) {
95+ services .services .put (service .getType () + ":" + service .getName (), service );
96+ } else {
97+ services .services .put (service .getType (), service );
7098 }
7199 }
72100 }
@@ -76,19 +104,17 @@ private String configBaseDirectory() {
76104 String envVariable = System .getenv ("LAMBDA_TASK_ROOT" );
77105 if (envVariable != null && envVariable .length () > 0 )
78106 return envVariable + File .separator ;
79-
107+
80108 if (ModelContext .getModelContext () != null ) {
81109 HttpContext webContext = (HttpContext ) ModelContext .getModelContext ().getHttpContext ();
82110 if ((webContext != null ) && (webContext instanceof HttpContextWeb )) {
83- baseDir = com .genexus .ModelContext .getModelContext ()
84- .getHttpContext ().getDefaultPath ()
85- + File .separator + "WEB-INF" + File .separatorChar ;
86- }
111+ baseDir = com .genexus .ModelContext .getModelContext ().getHttpContext ().getDefaultPath () + File .separator
112+ + "WEB-INF" + File .separatorChar ;
113+ }
87114 }
88115 if (baseDir .equals ("" )) {
89- String servletPath = com .genexus .ApplicationContext .getInstance ().getServletEngineDefaultPath ();
90- if (servletPath != null && !servletPath .equals ("" ))
91- {
116+ String servletPath = com .genexus .ApplicationContext .getInstance ().getServletEngineDefaultPath ();
117+ if (servletPath != null && !servletPath .equals ("" )) {
92118 baseDir = servletPath + File .separator + "WEB-INF" + File .separatorChar ;
93119 }
94120 }
@@ -99,62 +125,15 @@ private void readServices(String basePath) {
99125
100126 if (basePath .equals ("" ))
101127 basePath = configBaseDirectory ();
102- if (new File (basePath + SERVICES_DEV_FILE ).exists ()){
128+ if (new File (basePath + SERVICES_DEV_FILE ).exists ()) {
103129 loadFromFile (basePath , SERVICES_DEV_FILE , this );
104130 }
105- if (new File (basePath + SERVICES_FILE ).exists ()){
131+ if (new File (basePath + SERVICES_FILE ).exists ()) {
106132 loadFromFile (basePath , SERVICES_FILE , this );
107133 }
108134 }
109135
110- private void processService (XMLReader reader ) {
111- short result ;
112- result = reader .readType (1 , "Name" );
113- String name = new String (reader .getValue ());
114-
115- result = reader .readType (1 , "Type" );
116- String type = new String (reader .getValue ());
117-
118- result = reader .readType (1 , "ClassName" );
119- String className = new String (reader .getValue ());
120-
121- GXProperties properties = processProperties (reader );
122-
123- GXService service = new GXService ();
124- service .setName (name );
125- service .setType (type );
126- service .setClassName (className );
127- service .setProperties (properties );
128- if (services .containsKey (type )){
129- GXService prev = services .get (type );
130- services .remove (type );
131- services .put (prev .getType () + ":" + prev .getName (), prev );
132- services .put (service .getType () + ":" + service .getName (), service );
133- }
134- else {
135- services .put (type , service );
136- }
137- }
138-
139- private GXProperties processProperties (XMLReader reader ) {
140- short result ;
141- GXProperties properties = new GXProperties ();
142- reader .readType (1 , "Properties" );
143- reader .read ();
144- while (reader .getName ().equals ("Property" )) {
145- result = reader .readType (1 , "Name" );
146- String name = new String (reader .getValue ());
147- result = reader .readType (1 , "Value" );
148- String value = new String (reader .getValue ());
149- properties .add (name , value );
150- reader .read ();
151- reader .read ();
152- }
153- return properties ;
154- }
155-
156136 public GXService get (String name ) {
157137 return services .get (name );
158138 }
159-
160139}
0 commit comments