-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from OpenWiseSolutions/feature/OHFJIRA-20-stan…
…dalone-OHF OHFJIRA-20 - Standalone (executable) OHF running
- Loading branch information
Showing
15 changed files
with
612 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,6 @@ | |
*.ear | ||
|
||
# working directories | ||
target/ | ||
target/ | ||
ohf-app.pid | ||
ohf-app.port |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
common/src/main/java/org/openhubframework/openhub/common/log/LazyFileOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* Copyright 2017 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.openhubframework.openhub.common.log; | ||
|
||
import java.io.*; | ||
|
||
|
||
/** | ||
* Represents an output stream of bytes with LAZY strategy of stream initialization. | ||
* | ||
* @author Tomas Hanus | ||
* @since 2.0 | ||
*/ | ||
public class LazyFileOutputStream extends OutputStream { | ||
|
||
private final Object STREAM_LOCK = new Object(); | ||
|
||
private File file; | ||
private boolean append; | ||
private boolean streamOpen = false; | ||
private FileOutputStream oStream; | ||
|
||
/** | ||
* Create lazy file-based {@link OutputStream}. | ||
* | ||
* @param f as file used for flushing log events | ||
* @see #LazyFileOutputStream(File, boolean) | ||
*/ | ||
public LazyFileOutputStream(File f) { | ||
this.file = f; | ||
} | ||
|
||
/** | ||
* Create lazy file-based {@link OutputStream} where is possible to specify {@code append} flag. | ||
* | ||
* @param f as file used for flushing log events | ||
* @param append {@code true} if the file is opened for append | ||
* @see FileOutputStream#append | ||
* @see #LazyFileOutputStream(File) | ||
*/ | ||
public LazyFileOutputStream(File f, boolean append) { | ||
this.file = f; | ||
this.append = append; | ||
} | ||
|
||
/** | ||
* Create lazy file-based {@link OutputStream} by converting the given pathname string into an abstract pathname. | ||
* | ||
* @param pathName a pathname string | ||
* @see File#File(String) | ||
* @see #LazyFileOutputStream(String, boolean) | ||
*/ | ||
public LazyFileOutputStream(String pathName) { | ||
this(pathName != null ? new File(pathName) : null); | ||
} | ||
|
||
/** | ||
* Create lazy file-based {@link OutputStream} by converting the given pathname string into an abstract pathname | ||
* where is possible to specify {@code append} flag. | ||
* | ||
* @param pathName a pathname string | ||
* @param append {@code true} if the file is opened for append | ||
* @see File#File(String) | ||
* @see #LazyFileOutputStream(String) | ||
*/ | ||
public LazyFileOutputStream(String pathName, boolean append) { | ||
this(pathName != null ? new File(pathName) : null, append); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
super.close(); | ||
if (streamOpen) { | ||
outputStream().close(); | ||
} | ||
} | ||
|
||
@Override | ||
public void flush() throws IOException { | ||
super.flush(); | ||
|
||
if (streamOpen) { | ||
outputStream().flush(); | ||
} | ||
} | ||
|
||
@Override | ||
public void write(int b) throws IOException { | ||
outputStream().write(b); | ||
} | ||
|
||
@Override | ||
public void write(byte[] b, int off, int len) throws IOException { | ||
outputStream().write(b, off, len); | ||
} | ||
|
||
@Override | ||
public void write(byte[] b) throws IOException { | ||
outputStream().write(b); | ||
} | ||
|
||
/** | ||
* This method is the key component of the class, it gets the wrapped FileOutputStream object if already initialized | ||
* or if not it generates it in a thread safe way. This kind of implementation allows to call the initialization | ||
* of the underlying FileOutputStream object only when needed. | ||
* | ||
* @return the wrapped FileOutputStream object | ||
* @throws FileNotFoundException if the file can't be created | ||
*/ | ||
protected FileOutputStream outputStream() throws FileNotFoundException { | ||
synchronized (STREAM_LOCK) { | ||
if (!streamOpen) { | ||
oStream = new FileOutputStream(file, append); | ||
|
||
streamOpen = true; | ||
} | ||
} | ||
return oStream; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...src/main/java/org/openhubframework/openhub/common/log/LazyRollingFileLogbackAppender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2017 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.openhubframework.openhub.common.log; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import ch.qos.logback.core.rolling.RollingFileAppender; | ||
import ch.qos.logback.core.util.FileUtil; | ||
|
||
|
||
/** | ||
* Logback {@link RollingFileAppender} implementation that supports lazy initialization of appender. It is useful | ||
* for example when appender should be used only when | ||
* <a href="https://logback.qos.ch/manual/configuration.html#conditional">condition is satisfied</a>. This feature is | ||
* <a href="https://jira.qos.ch/browse/LOGBACK-202">not supported by default</a>. | ||
* | ||
* @author Tomas Hanus | ||
* @see LazyFileOutputStream | ||
* @since 2.0 | ||
*/ | ||
public class LazyRollingFileLogbackAppender<E> extends RollingFileAppender<E> { | ||
|
||
@Override | ||
public void openFile(String file_name) throws IOException { | ||
lock.lock(); | ||
try { | ||
File file = new File(file_name); | ||
boolean result = FileUtil.createMissingParentDirectories(file); | ||
if (!result) { | ||
addError("Failed to create parent directories for [" + file.getAbsolutePath() + "]"); | ||
} | ||
|
||
LazyFileOutputStream lazyFos = new LazyFileOutputStream(file, append); | ||
setOutputStream(lazyFos); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#!/bin/sh | ||
JARFile="openhub.war" | ||
PIDFile="ohf-app.pid" | ||
JVM_OPTS="-Xmx2g -Dserver.port=8080" | ||
SPRING_OPTS="--logging.file=ohf-app.log" | ||
|
||
function check_if_pid_file_exists { | ||
if [ ! -f $PIDFile ] | ||
then | ||
echo "PID file not found: $PIDFile" | ||
exit 1 | ||
fi | ||
} | ||
|
||
function check_if_process_is_running { | ||
if ps -p $(print_process) > /dev/null | ||
then | ||
return 0 | ||
else | ||
return 1 | ||
fi | ||
} | ||
|
||
function print_process { | ||
echo $(<"$PIDFile") | ||
} | ||
|
||
case "$1" in | ||
status) | ||
check_if_pid_file_exists | ||
if check_if_process_is_running | ||
then | ||
echo $(print_process)" is running" | ||
else | ||
echo "Process not running: $(print_process)" | ||
fi | ||
;; | ||
stop) | ||
check_if_pid_file_exists | ||
if ! check_if_process_is_running | ||
then | ||
echo "Process $(print_process) already stopped" | ||
exit 0 | ||
fi | ||
kill -TERM $(print_process) | ||
echo -ne "Waiting for process to stop" | ||
NOT_KILLED=1 | ||
for i in {1..20}; do | ||
if check_if_process_is_running | ||
then | ||
echo -ne "." | ||
sleep 1 | ||
else | ||
NOT_KILLED=0 | ||
fi | ||
done | ||
echo | ||
if [ $NOT_KILLED = 1 ] | ||
then | ||
echo "Cannot kill process $(print_process)" | ||
exit 1 | ||
fi | ||
echo "Process stopped" | ||
;; | ||
start) | ||
if [ -f $PIDFile ] && check_if_process_is_running | ||
then | ||
echo "Process $(print_process) already running" | ||
exit 1 | ||
fi | ||
nohup java $JVM_OPTS -jar $JARFile $SPRING_OPTS & | ||
echo "Process started" | ||
;; | ||
restart) | ||
$0 stop | ||
if [ $? = 1 ] | ||
then | ||
exit 1 | ||
fi | ||
$0 start | ||
;; | ||
*) | ||
echo "Usage: $0 {start|stop|restart|status}" | ||
exit 1 | ||
esac | ||
|
||
exit 0 |
Oops, something went wrong.