Skip to content

Commit

Permalink
Merge pull request #24 from OpenWiseSolutions/feature/OHFJIRA-20-stan…
Browse files Browse the repository at this point in the history
…dalone-OHF

OHFJIRA-20 - Standalone (executable) OHF running
  • Loading branch information
hanusto committed Feb 5, 2017
2 parents 9383556 + a0f2f2f commit 02291e5
Show file tree
Hide file tree
Showing 15 changed files with 612 additions and 50 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
*.ear

# working directories
target/
target/
ohf-app.pid
ohf-app.port
7 changes: 7 additions & 0 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!-- Custom lazy load file appender implementation -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>

<!-- Apache Camel -->
<dependency>
Expand Down
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;
}
}
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();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Profile;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.stereotype.Component;

import org.openhubframework.openhub.api.configuration.ConfigurableValue;
Expand All @@ -65,7 +65,7 @@
*/
@Component
@Profile("!" + Profiles.TEST) // not init for tests
public class ConfigurationChecker implements ApplicationListener<ContextRefreshedEvent> {
public class ConfigurationChecker implements ApplicationListener<ContextStartedEvent> {

private static final Logger LOG = LoggerFactory.getLogger(ConfigurationChecker.class);

Expand Down Expand Up @@ -98,7 +98,7 @@ public class ConfigurationChecker implements ApplicationListener<ContextRefreshe


@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
public void onApplicationEvent(ContextStartedEvent event) {
checkConfiguration(event.getApplicationContext());
}

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!-- OpenHub Spring Boot application -->
<start-class>org.openhubframework.openhub.admin.OpenHubApplication</start-class>
<start-class>org.openhubframework.openhub.OpenHubApplication</start-class>

<camel-version>2.17.3</camel-version>
<junit-version>4.12</junit-version>
Expand Down
87 changes: 87 additions & 0 deletions run-ohf.sh
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

0 comments on commit 02291e5

Please sign in to comment.