Skip to content

Commit

Permalink
Revert "add support for multiple Environments - not Singleton anymore"
Browse files Browse the repository at this point in the history
This reverts commit 348638e.
  • Loading branch information
ar committed May 4, 2019
1 parent d1dd821 commit 663507c
Show file tree
Hide file tree
Showing 28 changed files with 152 additions and 297 deletions.
47 changes: 32 additions & 15 deletions jpos/src/main/java/org/jpos/core/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,55 @@ public class Environment implements Loggeable {
private static final String ENVIRONMENT_PREFIX = "env";
private static Pattern valuePattern = Pattern.compile("^([\\w\\W]*)(\\$)([\\w\\W]*)?\\{([\\w\\W]+)\\}([\\w\\W]*)$");
private static Pattern verbPattern = Pattern.compile("^\\$verb\\{([\\w\\W]+)\\}$");
private static Environment INSTANCE;
private String name;
private AtomicReference<Properties> propRef = new AtomicReference<>(new Properties());
private static String SP_PREFIX = "system.property.";
private static int SP_PREFIX_LENGTH = SP_PREFIX.length();

public Environment() {
this(System.getProperty ("jpos.env"));

static {
try {
INSTANCE = new Environment();
} catch (Throwable e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}

public Environment(String name) {
this.name = name == null ? "default" : name;
private Environment() throws IOException {
name = System.getProperty ("jpos.env");
name = name == null ? "default" : name;
readConfig ();
}

public boolean isProduction() {
return name.toLowerCase().startsWith(("prod"));
}

public String getName() {
return name;
}

public String get (String p) {
return getProperty(p, p);
public static Environment reload() throws IOException {
return (INSTANCE = new Environment());
}

public static Environment getEnvironment() {
return INSTANCE;
}
public String get (String p, String def) {
return getProperty(p, def);
public static String get (String p) {
return getEnvironment().getProperty(p, p);
}
public static String get (String p, String def) {
return getEnvironment().getProperty(p, def);
}
public String getProperty (String p, String def) {
String s = getProperty (p);
return s != null ? s : def;
}


/**
* If property name has the pattern <code>${propname}</code>, this method will
*
Expand Down Expand Up @@ -124,14 +144,11 @@ public String getProperty (String s) {
}

@SuppressWarnings("unchecked")
private void readConfig () {
private void readConfig () throws IOException {
if (name != null) {
try {
if (!readYAML())
readCfg();
} catch (IOException e) {
throw new RuntimeException (e);
}
if (!readYAML())
readCfg();

extractSystemProperties();
}
}
Expand Down
37 changes: 6 additions & 31 deletions jpos/src/main/java/org/jpos/core/SimpleConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,20 @@
*/
public class SimpleConfiguration implements Configuration, Serializable {
private Properties props;
private transient Environment env;

public SimpleConfiguration () {
props = new Properties();
env = new Environment();
}

public SimpleConfiguration(Environment env) {
Objects.requireNonNull(env, "Invalid environment");
this.env = env;
}

public SimpleConfiguration (Properties props) {
this.props = props;
env = new Environment();
}
public SimpleConfiguration (String filename) throws IOException {
props = new Properties();
env = new Environment();
load (filename);
}

public SimpleConfiguration (String filename, Environment env) throws IOException {
this.env = env;
Objects.requireNonNull(env, "Invalid environment");
public SimpleConfiguration (String filename)
throws IOException
{
props = new Properties();
load (filename);
}

public SimpleConfiguration(Properties props, Environment env) {
this.props = props;
this.env = env;
Objects.requireNonNull(env, "Invalid environment");
}

/**
* Returns the value of the configuration property named <tt>name</tt>, or the default value <tt>def</tt>.
*
Expand All @@ -90,7 +69,7 @@ public String get (String name, String def) {
List l = (List) obj;
obj = l.size() > 0 ? l.get(0) : null;
}
return (obj instanceof String) ? env.get((String) obj, def) : def;
return (obj instanceof String) ? Environment.get((String) obj, def) : def;
}
public String[] getAll (String name) {
String[] ret;
Expand All @@ -103,6 +82,7 @@ public String[] getAll (String name) {
} else
ret = new String[0];

Environment env = Environment.getEnvironment();
IntStream.range(0, ret.length).forEachOrdered(i -> ret[i] = env.getProperty(ret[i]));
return Arrays.stream(ret).filter(Objects::nonNull).toArray(String[]::new);
}
Expand Down Expand Up @@ -199,10 +179,5 @@ public String toString() {
'}';
}

Object readResolve() {
env = new Environment();
return this;
}

private static final long serialVersionUID = -9081631781490453993L;
private static final long serialVersionUID = -6361797037366246968L;
}
3 changes: 1 addition & 2 deletions jpos/src/main/java/org/jpos/q2/ConfigurationFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
import org.jdom2.Element;
import org.jpos.core.Configuration;
import org.jpos.core.ConfigurationException;
import org.jpos.core.Environment;

public interface ConfigurationFactory {
Configuration getConfiguration(Element e, Environment env)
Configuration getConfiguration(Element e)
throws ConfigurationException;
}
19 changes: 10 additions & 9 deletions jpos/src/main/java/org/jpos/q2/Q2.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@
import java.time.Instant;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static java.util.ResourceBundle.getBundle;

Expand Down Expand Up @@ -135,7 +137,6 @@ public class Q2 implements FileFilter, Runnable {
private String sshAuthorizedKeys;
private String sshUser;
private String sshHostKeyFile;
private Environment env;
private static String DEPLOY_PREFIX = "META-INF/q2/deploy/";
private static String CFG_PREFIX = "META-INF/q2/cfg/";

Expand Down Expand Up @@ -357,9 +358,6 @@ public static Q2 getQ2() {
public static Q2 getQ2(long timeout) {
return (Q2) NameRegistrar.get(JMX_NAME, timeout);
}
public Environment getEnvironment() {
return env;
}

private boolean isXml(File f) {
return f != null && f.getName().toLowerCase().endsWith(".xml");
Expand Down Expand Up @@ -585,7 +583,7 @@ private boolean deploy (File f) {
String enabledAttribute = rootElement.getAttributeValue("enabled", "true");
if ("true".equalsIgnoreCase(enabledAttribute) ||
"yes".equalsIgnoreCase(enabledAttribute) ||
enabledAttribute.contains(env.getName()))
enabledAttribute.contains(Environment.getEnvironment().getName()))
{
if (evt != null)
evt.addMessage("deploy: " + f.getCanonicalPath());
Expand Down Expand Up @@ -773,9 +771,8 @@ private void parseCmdLine (String[] args) {
if (line.hasOption("n"))
name = line.getOptionValue("n");
if (line.hasOption("E")) {
env = new Environment (line.getOptionValue("E"));
} else {
env = new Environment();
System.setProperty("jpos.env", line.getOptionValue("E"));
Environment.getEnvironment();
}
disableDeployScan = line.hasOption("Ns");
disableDynamicClassloader = line.hasOption("Nd");
Expand Down Expand Up @@ -1132,7 +1129,11 @@ private boolean waitForChanges (WatchService service) throws InterruptedExceptio
);
return false; // deploy directory no longer valid
}
env = new Environment(env.getName());
try {
Environment.reload();
} catch (IOException e) {
getLog().warn(e);
}
}
return true;
}
Expand Down
6 changes: 0 additions & 6 deletions jpos/src/main/java/org/jpos/q2/QBeanSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.jpos.core.Configurable;
import org.jpos.core.Configuration;
import org.jpos.core.ConfigurationException;
import org.jpos.core.Environment;
import org.jpos.util.*;

import java.beans.BeanInfo;
Expand Down Expand Up @@ -64,15 +63,10 @@ public void setServer (Q2 server) {
this.server = server;
}

public Environment getEnvironment() {
return server.getEnvironment();
}

@Override
public Q2 getServer () {
return server;
}

public QFactory getFactory () {
return getServer().getFactory ();
}
Expand Down
8 changes: 4 additions & 4 deletions jpos/src/main/java/org/jpos/q2/QFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ else if ("boolean".equals (type))
type = "java.lang.Boolean";

String value = childElement.getText();
value = q2 != null ? q2.getEnvironment().getProperty(value, value) : value;
value = Environment.getEnvironment().getProperty(value, value);
try {
Class attributeType = Class.forName(type);
if(Collection.class.isAssignableFrom(attributeType))
Expand Down Expand Up @@ -334,7 +334,7 @@ public Configuration getConfiguration (Element e)
ConfigurationFactory cf = configurationFactoryClazz != null ?
(ConfigurationFactory) newInstance(configurationFactoryClazz) : defaultConfigurationFactory;

Configuration cfg = cf.getConfiguration(e, q2.getEnvironment());
Configuration cfg = cf.getConfiguration(e);
String merge = getAttributeValue(e, "merge-configuration");
if (merge != null) {
StringTokenizer st = new StringTokenizer(merge, ", ");
Expand Down Expand Up @@ -376,9 +376,9 @@ public void setLogger (Object obj, Element e) {
}
}

public String getAttributeValue (Element e, String name) {
public static String getAttributeValue (Element e, String name) {
String s = e.getAttributeValue(name);
return q2.getEnvironment().getProperty(s, s);
return Environment.getEnvironment().getProperty(s, s);
}
public void setConfiguration (Object obj, Element e)
throws ConfigurationException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.jdom2.Element;
import org.jpos.core.Configuration;
import org.jpos.core.ConfigurationException;
import org.jpos.core.Environment;
import org.jpos.core.SimpleConfiguration;

import java.io.File;
Expand All @@ -30,7 +29,7 @@
import java.util.Properties;

public class SimpleConfigurationFactory implements ConfigurationFactory {
public Configuration getConfiguration(Element e, Environment env) throws ConfigurationException {
public Configuration getConfiguration(Element e) throws ConfigurationException {
Properties props = new Properties ();
Iterator iter = e.getChildren ("property").iterator();
while (iter.hasNext()) {
Expand Down Expand Up @@ -61,6 +60,6 @@ else if (name != null && value != null) {
props.put (name, value);
}
}
return new SimpleConfiguration(props, env);
return new SimpleConfiguration(props);
}
}
5 changes: 2 additions & 3 deletions jpos/src/main/java/org/jpos/q2/cli/ENV.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@
import org.jpos.q2.CLICommand;
import org.jpos.q2.CLIContext;
import org.jline.terminal.Terminal;
import org.jpos.q2.Q2;

@SuppressWarnings("unused")
public class ENV implements CLICommand {
public void exec(CLIContext cli, String[] args) throws Exception {
Q2 q2 = Q2.getQ2();
Terminal term = cli.getReader().getTerminal();
cli.println ("TERM=" + term.getClass().getSimpleName() + "/" + term.getType());
cli.println (q2.getEnvironment().toString());
cli.println ("PRODUCTION=" + Environment.getEnvironment().isProduction());
cli.println (Environment.getEnvironment().toString());
}
}
Loading

0 comments on commit 663507c

Please sign in to comment.