Skip to content
This repository was archived by the owner on Apr 15, 2021. It is now read-only.

Commit 286c600

Browse files
author
stavreva
committed
Add initializer for log properties in vmware
1 parent 8df65a0 commit 286c600

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/*******************************************************************************
2+
*
3+
* Copyright FUJITSU LIMITED 2017
4+
*
5+
* Creation Date: 2014-05-16
6+
*
7+
*******************************************************************************/
8+
9+
package org.oscm.app.vmware.service;
10+
11+
import java.io.File;
12+
import java.io.IOException;
13+
import java.io.InputStream;
14+
import java.nio.charset.Charset;
15+
import java.nio.charset.StandardCharsets;
16+
import java.nio.file.Files;
17+
import java.nio.file.Path;
18+
import java.nio.file.Paths;
19+
import java.util.Collection;
20+
21+
import javax.annotation.PostConstruct;
22+
import javax.annotation.Resource;
23+
import javax.ejb.Singleton;
24+
import javax.ejb.Startup;
25+
import javax.ejb.Timeout;
26+
import javax.ejb.Timer;
27+
import javax.ejb.TimerService;
28+
import javax.inject.Inject;
29+
30+
import org.apache.commons.io.FileUtils;
31+
import org.apache.commons.io.IOUtils;
32+
import org.apache.log4j.LogManager;
33+
import org.apache.log4j.PropertyConfigurator;
34+
import org.oscm.app.v2_0.intf.ControllerAccess;
35+
import org.slf4j.Logger;
36+
import org.slf4j.LoggerFactory;
37+
38+
@Singleton
39+
@Startup
40+
public class Initializer {
41+
42+
private static final Logger LOGGER = LoggerFactory.getLogger(Initializer.class);
43+
44+
// Default name of log4j template
45+
private String LOG4J_TEMPLATE = "log4j.properties.template";
46+
47+
// Timer based log file monitoring
48+
private long TIMER_DELAY_VALUE = 60000;
49+
50+
@Resource
51+
private TimerService timerService;
52+
private File logFile;
53+
private long logFileLastModified = 0;
54+
private boolean logFileWarning = false;
55+
56+
@Inject
57+
private ControllerAccess controllerAccess;
58+
59+
public void setControllerAccess(final ControllerAccess controllerAccess) {
60+
this.controllerAccess = controllerAccess;
61+
}
62+
63+
@PostConstruct
64+
private void postConstruct() {
65+
try {
66+
// Get default config folder of GF instance
67+
String instanceRoot = System.getProperty("com.sun.aas.instanceRoot");
68+
String controllerId = controllerAccess.getControllerId();
69+
if (instanceRoot != null) {
70+
File root = new File(instanceRoot);
71+
if (root.isDirectory()) {
72+
String filePath = "/config/log4j." + controllerId + ".properties";
73+
// Determine log file
74+
logFile = new File(root, filePath);
75+
76+
// If the target file does not exist we will provide it once
77+
// from the template
78+
if (!logFile.exists()) {
79+
publishTemplateFile();
80+
} else {
81+
replacePackageName(instanceRoot + filePath);
82+
}
83+
84+
// Read configuration
85+
handleOnChange(logFile);
86+
87+
// And init timer based service
88+
LOGGER.debug("Enable timer service for monitoring modification of " + logFile.getPath());
89+
initTimer();
90+
91+
} else {
92+
LOGGER.error("Failed to initialize log file: invalid instanceRoot " + instanceRoot);
93+
logFile = null;
94+
}
95+
} else {
96+
LOGGER.error("Failed to initialize log file: missing system property 'com.sun.aas.instanceRoot'");
97+
}
98+
} catch (Exception e) {
99+
LOGGER.error("Failed to initialize log file", e);
100+
logFile = null;
101+
}
102+
}
103+
104+
/**
105+
* Initialize timer service.
106+
*/
107+
private void initTimer() {
108+
Collection<?> timers = timerService.getTimers();
109+
if (timers.isEmpty()) {
110+
timerService.createTimer(0, TIMER_DELAY_VALUE, null);
111+
}
112+
}
113+
114+
/**
115+
* Copy template file to default destination
116+
*/
117+
private void publishTemplateFile() throws Exception {
118+
InputStream is = null;
119+
try {
120+
// Search resource in controller package
121+
is = controllerAccess.getClass().getClassLoader().getResourceAsStream(LOG4J_TEMPLATE);
122+
if (is == null) {
123+
LOGGER.warn("Template file not found: " + LOG4J_TEMPLATE);
124+
} else if (logFile.getParentFile().exists()) {
125+
FileUtils.writeByteArrayToFile(logFile, IOUtils.toByteArray(is));
126+
}
127+
} catch (Exception e) {
128+
// ignore
129+
LOGGER.error("Failed to publish template file from " + LOG4J_TEMPLATE + " to " + logFile.getAbsolutePath(),
130+
e);
131+
} finally {
132+
if (is != null) {
133+
is.close();
134+
}
135+
}
136+
}
137+
138+
/**
139+
* Replace the package names from "com.fujitsu.bss.app" to "org.oscm.app" in
140+
* the existing log files.
141+
*/
142+
private void replacePackageName(String filePath) {
143+
try {
144+
Path path = Paths.get(filePath);
145+
Charset charset = StandardCharsets.UTF_8;
146+
String content = new String(Files.readAllBytes(path), charset);
147+
content = content.replaceAll("com.fujitsu.bss.app", "org.oscm.app");
148+
Files.write(path, content.getBytes(charset));
149+
} catch (IOException e) {
150+
e.printStackTrace();
151+
}
152+
153+
}
154+
155+
/**
156+
* Handles the timer event.
157+
*/
158+
@Timeout
159+
public void handleTimer(@SuppressWarnings("unused") Timer timer) {
160+
if (logFile != null) {
161+
handleOnChange(logFile);
162+
}
163+
}
164+
165+
/**
166+
* On change event
167+
*/
168+
void handleOnChange(File logFile) {
169+
try {
170+
long lastModif = logFile.lastModified();
171+
if (lastModif > logFileLastModified) {
172+
logFileLastModified = lastModif;
173+
LOGGER.debug("Reload log4j configuration from " + logFile.getAbsolutePath());
174+
new PropertyConfigurator().doConfigure(logFile.getAbsolutePath(), LogManager.getLoggerRepository());
175+
logFileWarning = false;
176+
}
177+
} catch (Exception e) {
178+
if (!logFileWarning) {
179+
logFileWarning = true;
180+
LOGGER.error(logFile.getAbsolutePath(), e);
181+
}
182+
}
183+
}
184+
}

0 commit comments

Comments
 (0)