Commit aeb3d81a by natalya

Первая версия завершена

parent 781e2b50
package kz.arta.ext.jdm;
import kz.arta.ext.jdm.after.DeploymentsDirectoryObserver;
import kz.arta.ext.jdm.before.DeploymentsMover;
import kz.arta.ext.jdm.config.Config;
/**
* Created by timur on 10/12/16.
*/
public class MainClass {
public static void main(String[] args) {
Config.getInstance().readParams(args);
if (Config.getInstance().getCurrentOption().equals("before")) {
new DeploymentsMover().move();
} else {
new DeploymentsDirectoryObserver().start();
}
}
}
package kz.arta.ext.jdm.config;
package kz.arta.ext.jdm.after;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Created by timur on 10/10/16.
*/
public class DeploymentsDirectoryObserver extends Thread {
private static final Logger LOGGER = LoggerFactory.getLogger(DeploymentsDirectoryObserver.class);
private DirectoryViewer viewer = new DirectoryViewer();
private FileUtil fileUtil = new FileUtil();
@Override
public void run() {
while (!viewer.checkDeploymentsDir()) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
LOGGER.error(e.getMessage(), e);
}
}
}
......
package kz.arta.ext.jdm.config;
package kz.arta.ext.jdm.after;
import kz.arta.ext.jdm.config.model.Deployment;
import kz.arta.ext.jdm.config.model.Deployments;
......@@ -13,16 +13,16 @@ import java.util.List;
public class DeploymentsList {
private HashMap <String, DeploymentItem> items = new HashMap<>();
private HashMap <String, Item> items = new HashMap<>();
public DeploymentsList(Deployments deployments) {
for (Deployment deployment: deployments.getDeployments()) {
DeploymentItem item = null;
Item item = null;
if (items.containsKey(deployment.getName())) {
item = items.get(deployment.getName());
} else {
item = new DeploymentItem();
item = new Item();
item.fileName = deployment.getName();
items.put(item.fileName, item);
}
......@@ -31,12 +31,51 @@ public class DeploymentsList {
}
private class DeploymentItem {
public List<Item> getAllList() {
return new ArrayList<>(items.values());
}
public List<Item> getNotDeployedList() {
List<Item> list = new ArrayList<>();
for (String key: items.keySet()){
if (!items.get(key).isDeployed()) {
list.add(items.get(key));
}
}
return list;
}
public class Item {
String fileName;
List<String> dependsOnList = new ArrayList<>();
boolean isDeployed;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public List<String> getDependsOnList() {
return dependsOnList;
}
public void setDependsOnList(List<String> dependsOnList) {
this.dependsOnList = dependsOnList;
}
public boolean isDeployed() {
return isDeployed;
}
public void setDeployed(boolean deployed) {
isDeployed = deployed;
}
}
}
package kz.arta.ext.jdm.after;
import kz.arta.ext.jdm.common.FileUtil;
import kz.arta.ext.jdm.config.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.List;
/**
* Created by timur on 10/10/16.
*/
public class DirectoryViewer {
private static final Logger LOGGER = LoggerFactory.getLogger(DirectoryViewer.class);
public boolean checkDeploymentsDir() {
LOGGER.info("Looking for newly deployed files...");
List<DeploymentsList.Item> items = Config.getInstance().getDeploymentsList().getNotDeployedList();
LOGGER.debug(String.format("Number of not satisfied deployments is %s", items.size()));
for (DeploymentsList.Item item: items) {
boolean satisfied = true;
for (String dependency: item.getDependsOnList()) {
if (!isDeployed(dependency)) {
satisfied = false;
}
}
if (satisfied) {
LOGGER.debug(String.format("Newly satisfied dependency %s", item.getFileName()));
new FileUtil().moveFileOnDeployed(item.fileName);
item.setDeployed(true);
}
}
items = Config.getInstance().getDeploymentsList().getNotDeployedList();
LOGGER.debug(String.format("Number of not satisfied dependencies now is %s", items.size()));
return items.size() == 0;
}
private boolean isDeployed(String fileName) {
File deployedFile = new File(Config.getInstance().getDeploymentsPath(), fileName + ".deployed");
return deployedFile.exists();
}
}
package kz.arta.ext.jdm.before;
import kz.arta.ext.jdm.after.DeploymentsList;
import kz.arta.ext.jdm.common.FileUtil;
import kz.arta.ext.jdm.config.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
/**
* Created by timur on 10/12/16.
*/
public class DeploymentsMover {
private static final Logger LOGGER = LoggerFactory.getLogger(DeploymentsMover.class);
public void move() {
LOGGER.debug("Start moving files with deployment dependencies");
FileUtil fileUtil = new FileUtil();
List<DeploymentsList.Item> items = Config.getInstance().getDeploymentsList().getAllList();
LOGGER.debug(String.format("Files list size is %s", items.size()));
for (DeploymentsList.Item item: items) {
LOGGER.debug(String.format("Moving %s", item.getFileName()));
fileUtil.moveFileBeforeStart(item.getFileName());
}
}
}
package kz.arta.ext.jdm.config;
package kz.arta.ext.jdm.common;
import kz.arta.ext.jdm.config.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -13,18 +14,20 @@ public class FileUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(FileUtil.class);
public void moveFileBeforeStart(String fileName) {
LOGGER.trace(String.format("Move %s to tmp directory", fileName));
try {
Files.move(FileSystems.getDefault().getPath(Config.getInstance().getDeploymentsPath(), fileName),
FileSystems.getDefault().getPath(Config.getInstance().getTmpPath()), StandardCopyOption.REPLACE_EXISTING);
FileSystems.getDefault().getPath(Config.getInstance().getTmpPath(), fileName), StandardCopyOption.REPLACE_EXISTING);
} catch (Exception exc) {
LOGGER.error(exc.getMessage(), exc);
}
}
public void moveFileOnDeployed(String fileName) {
LOGGER.trace(String.format("Move %s to deployments directory", fileName));
try {
Files.move(FileSystems.getDefault().getPath(Config.getInstance().getTmpPath(), fileName),
FileSystems.getDefault().getPath(Config.getInstance().getDeploymentsPath()), StandardCopyOption.REPLACE_EXISTING);
FileSystems.getDefault().getPath(Config.getInstance().getDeploymentsPath(), fileName), StandardCopyOption.REPLACE_EXISTING);
} catch (Exception exc) {
LOGGER.error(exc.getMessage(), exc);
}
......
package kz.arta.ext.jdm.config;
import kz.arta.ext.jdm.config.model.Deployment;
import kz.arta.ext.jdm.after.DeploymentsList;
import kz.arta.ext.jdm.config.model.Deployments;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -24,10 +24,12 @@ public class Config {
private String tmpPath = "/opt/tmpDeployments";
private Deployments deployments;
private String currentOption = "before";
private DeploymentsList deploymentsList;
private Config() {
readConfig();
}
private static Config config = null;
......@@ -53,7 +55,8 @@ public class Config {
bout.write(buffer, 0, read);
}
deployments = JAXB.unmarshal(new ByteArrayInputStream(bout.toByteArray()), Deployments.class);
Deployments deployments = JAXB.unmarshal(new ByteArrayInputStream(bout.toByteArray()), Deployments.class);
deploymentsList = new DeploymentsList(deployments);
} catch (Exception exc){
LOGGER.error(exc.getMessage(), exc);
......@@ -74,7 +77,33 @@ public class Config {
return tmpPath;
}
public Deployments getDeployments() {
return deployments;
public String getCurrentOption() {
return currentOption;
}
public void setCurrentOption(String currentOption) {
this.currentOption = currentOption;
}
public DeploymentsList getDeploymentsList() {
return deploymentsList;
}
public void readParams(String [] args) {
try {
currentOption = args[0];
filePath = args[1];
tmpPath = args[2];
deploymentsPath = args[3];
} catch (Exception exc){
System.err.println("You should pass 4 parameters:\n" +
" 0: option (before - pass this value before jboss starts, this will move all files with dependencies from deployments to tmp directory; \n" +
" \tafter - pass this value after jboss started, this will observe deployments directory and move files from tmp to deployments when needed) \n" +
" 1: path to config file with deployment dependencies\n" +
" 2: path to tmp directory\n" +
" 3: path to deployments directory\n");
throw new RuntimeException("Invalid parameters", exc);
}
readConfig();
}
}
package kz.arta.ext.jdm.config;
import kz.arta.ext.jdm.config.model.Deployment;
import kz.arta.ext.jdm.config.model.Deployments;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
/**
* Created by timur on 10/10/16.
*/
public class DirectoryViewer {
private List<String> getDeployedList() {
String[] files = new File(Config.getInstance().getDeploymentsPath()).list();
HashSet<String> deploymentsSet = new HashSet();
HashSet<String> serviceFilesSet = new HashSet();
for (String file: files) {
if (file.endsWith("ear") || file.endsWith("jar") || file.endsWith("war")) {
deploymentsSet.add(file);
} else {
serviceFilesSet.add(file);
}
}
List<String> deployedList = new ArrayList<String>();
for (String str: deploymentsSet) {
if (serviceFilesSet.contains(str + ".deployed")) {
deployedList.add(str);
}
}
return deployedList;
}
public List<String> getReadyToDeployList() {
List<String> deployed = getDeployedList();
Deployments deployments = Config.getInstance().getDeployments();
for (Deployment deployment: deployments.getDeployments()) {
}
return null;
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
<appender name="info-out" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${jdm.logs.dir}/logs/jdm_info.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd.MM.yyyy HH:mm:ss} - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="info" />
<param name="AcceptOnMatch" value="true"/>
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter" />
</appender>
<appender name="debug-out" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${jdm.logs.dir}/logs/jdm_info.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd.MM.yyyy HH:mm:ss} - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="debug" />
<param name="AcceptOnMatch" value="true"/>
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter" />
</appender>
<appender name="error-out" class="org.apache.log4j.DailyRollingFileAppender">
<param name="Append" value="false"/>
<param name="File" value="${jdm.logs.dir}/logs/jdm_error.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd.MM.yyyy HH:mm:ss} - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="error" />
<param name="AcceptOnMatch" value="true"/>
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter" />
</appender>
<logger name="kz.arta.ext.jdm">
<param name="additivity" value="true"/>
<appender-ref ref="info-out" />
<appender-ref ref="debug-out" />
<appender-ref ref="error-out" />
</logger>
<root>
<level value="debug"/>
</root>
</log4j:configuration>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment