Attached Files | 33643.diff [^] (13,201 bytes) 2016-08-07 10:30 [Show Content] [Hide Content]diff --git a/src/org/openbravo/mobile/core/authenticate/MobileAuthenticationKeyUtils.java b/src/org/openbravo/mobile/core/authenticate/MobileAuthenticationKeyUtils.java
--- a/src/org/openbravo/mobile/core/authenticate/MobileAuthenticationKeyUtils.java
+++ b/src/org/openbravo/mobile/core/authenticate/MobileAuthenticationKeyUtils.java
@@ -9,6 +9,7 @@
package org.openbravo.mobile.core.authenticate;
+import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
@@ -190,10 +191,12 @@
final AuthenticationToken token = new AuthenticationToken(decryptedString);
if ((System.currentTimeMillis() - token.timestamp) > getMaxTokenAge()) {
- throw new AuthenticationException("Invalid token");
+ throw new AuthenticationException("Invalid token " + encryptedToken);
}
return new AuthenticationToken(decryptedString);
+ } catch (GeneralSecurityException se) {
+ throw new AuthenticationException("Invalid token " + encryptedToken, se);
} catch (AuthenticationException aex) {
throw aex;
} catch (Exception e) {
diff --git a/src/org/openbravo/mobile/core/servercontroller/MobileServerController.java b/src/org/openbravo/mobile/core/servercontroller/MobileServerController.java
--- a/src/org/openbravo/mobile/core/servercontroller/MobileServerController.java
+++ b/src/org/openbravo/mobile/core/servercontroller/MobileServerController.java
@@ -15,6 +15,7 @@
import java.util.List;
import java.util.Set;
+import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
@@ -22,10 +23,16 @@
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.openbravo.base.exception.OBException;
+import org.openbravo.base.model.Entity;
+import org.openbravo.base.model.ModelProvider;
import org.openbravo.base.provider.OBProvider;
import org.openbravo.base.session.OBPropertiesProvider;
import org.openbravo.base.util.Check;
import org.openbravo.base.weld.WeldUtils;
+import org.openbravo.client.kernel.event.EntityNewEvent;
+import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
+import org.openbravo.client.kernel.event.EntityUpdateEvent;
+import org.openbravo.client.kernel.event.TransactionCompletedEvent;
import org.openbravo.dal.core.OBContext;
import org.openbravo.dal.service.OBDal;
import org.openbravo.dal.service.OBQuery;
@@ -43,8 +50,6 @@
private final static Logger log = Logger.getLogger(MobileServerController.class);
- private String thisServerDefinitionId = null;
-
private static MobileServerController instance = new MobileServerController();
public static MobileServerController getInstance() {
@@ -59,6 +64,14 @@
private final String mobileServerKey;
private final boolean isMultiServer;
+ private String thisServerDefinitionId = null;
+ private String thisServerType = null;
+
+ private MobileServerState currentStatus = null;
+
+ private String centralServerKey = null;
+ private MobileServerState centralServerStatus = null;
+
public MobileServerController() {
// get mobile.server.key from the Openbravo.properties
mobileServerKey = OBPropertiesProvider.getInstance().getOpenbravoProperties()
@@ -67,6 +80,41 @@
}
/**
+ * @return the mobile server key of the current server.
+ */
+ public String getThisMobileServerKey() {
+ return mobileServerKey;
+ }
+
+ /**
+ * @return the key of the cental server, returns null if multi-server not enabled.
+ */
+ public String getCentralServerKey() {
+ if (centralServerKey == null) {
+ getCentralServer();
+ }
+ return centralServerKey;
+ }
+
+ /**
+ * @return the state of the central server, returns null if not set.
+ */
+ public MobileServerState getCentralServerState() {
+ MobileServerState tmpStatus = centralServerStatus;
+ if (tmpStatus != null) {
+ return tmpStatus;
+ }
+
+ final MobileServerDefinition centralServer = getCentralServer();
+ if (centralServer != null) {
+ tmpStatus = MobileServerState.getMobileServerState(centralServer.getStatus());
+ centralServerStatus = tmpStatus;
+ return tmpStatus;
+ }
+ return null;
+ }
+
+ /**
* Return the central server, assuming that there is only one active one for the client.
*
* Is assumed to be called from the store. Note:
@@ -119,15 +167,12 @@
if (result == null) {
throw new OBException("No central server defined/reachable");
}
+ centralServerKey = result.getMobileServerKey();
return result;
}
public boolean isCentralServerOnline() {
- final MobileServerDefinition centralServer = getCentralServer();
- if (centralServer == null) {
- return false;
- }
- return MobileServerState.ONLINE.getValue().equals(centralServer.getStatus());
+ return MobileServerState.ONLINE.getValue().equals(getCentralServerState());
}
/**
@@ -242,7 +287,7 @@
*/
public void writeServerStatusJSON(Writer w) {
try {
- final MobileServerState state = getMobileServerState(getThisServerDefinition());
+ final MobileServerState state = getThisMobileServerState();
if (state == null) {
return;
}
@@ -258,7 +303,7 @@
*/
public void setServerStatusJSON(JSONObject json) {
try {
- final MobileServerState state = getMobileServerState(getThisServerDefinition());
+ final MobileServerState state = getThisMobileServerState();
if (state == null) {
return;
}
@@ -279,7 +324,7 @@
return false;
}
- final MobileServerState state = getMobileServerState(getThisServerDefinition());
+ final MobileServerState state = getThisMobileServerState();
if (state == null) {
return false;
}
@@ -288,7 +333,23 @@
}
public MobileServerState getThisMobileServerState() {
- return getMobileServerState(getThisServerDefinition());
+ // use intermediate variable to prevent multi-threading issues with parallel
+ // resetting
+ MobileServerState tmpStatus = currentStatus;
+ if (tmpStatus != null) {
+ return tmpStatus;
+ }
+ tmpStatus = getMobileServerState(getThisServerDefinition());
+ currentStatus = tmpStatus;
+ return tmpStatus;
+ }
+
+ /**
+ * Reset the cached status, will be automatically reread at first use
+ */
+ public void resetCachedInformation() {
+ currentStatus = null;
+ centralServerStatus = null;
}
/**
@@ -344,7 +405,7 @@
* Reads the id of the {@link MobileServerDefinition} for the current server and returns it. Does
* not cache the instance.
*/
- private String getThisServerDefinitionId() {
+ public String getThisServerDefinitionId() {
if (mobileServerKey == null) {
return null;
@@ -358,6 +419,7 @@
OBContext.setAdminMode(false);
MobileServerDefinition serverDefinition = getMobileServer(mobileServerKey);
thisServerDefinitionId = serverDefinition.getId();
+ thisServerType = serverDefinition.getServerType();
return thisServerDefinitionId;
} finally {
OBContext.restorePreviousMode();
@@ -400,7 +462,7 @@
public void checkStartServerStateLogic() {
checkMultiServer();
- final MobileServerState serverState = getMobileServerState(getThisServerDefinition());
+ final MobileServerState serverState = getThisMobileServerState();
if (MobileServerState.TRANSITION_TO_ONLINE.equals(serverState)) {
// the transition to online is not being executed, restart it
@@ -462,6 +524,10 @@
return false;
}
+ if (thisServerType != null) {
+ return MobileServerUtils.STORE_SERVER.equals(thisServerType);
+ }
+
final MobileServerDefinition serverDefinition = getThisServerDefinition();
if (serverDefinition == null) {
return false;
@@ -474,6 +540,11 @@
return false;
}
+ if (thisServerType != null) {
+ return MobileServerUtils.MAIN_SERVER.equals(thisServerType);
+ }
+
+ // this will set the thisServerType
final MobileServerDefinition serverDefinition = getThisServerDefinition();
if (serverDefinition == null) {
return false;
@@ -575,4 +646,38 @@
this.obContext = obContext;
}
}
+
+ public static class MobileServerDefinitionHandler extends EntityPersistenceEventObserver {
+
+ private static Entity[] entities = { ModelProvider.getInstance().getEntity(
+ MobileServerDefinition.ENTITY_NAME) };
+
+ private ThreadLocal<Boolean> doReset = new ThreadLocal<Boolean>();
+
+ @Override
+ protected Entity[] getObservedEntities() {
+ return entities;
+ }
+
+ public void onCreate(@Observes EntityNewEvent event) {
+ if (!isValidEvent(event)) {
+ return;
+ }
+ doReset.set(true);
+ }
+
+ public void onUpdate(@Observes EntityUpdateEvent event) {
+ if (!isValidEvent(event)) {
+ return;
+ }
+ doReset.set(true);
+ }
+
+ public void onTransactionCompleted(@Observes TransactionCompletedEvent event) {
+ if (doReset.get() != null && doReset.get()) {
+ MobileServerController.getInstance().resetCachedInformation();
+ }
+ doReset.set(null);
+ }
+ }
}
diff --git a/src/org/openbravo/mobile/core/servercontroller/MobileServerRequestExecutor.java b/src/org/openbravo/mobile/core/servercontroller/MobileServerRequestExecutor.java
--- a/src/org/openbravo/mobile/core/servercontroller/MobileServerRequestExecutor.java
+++ b/src/org/openbravo/mobile/core/servercontroller/MobileServerRequestExecutor.java
@@ -220,9 +220,8 @@
* server.
*/
public JSONObject executeCentralRequest(String serviceName, JSONObject parameters) {
- final MobileServerDefinition centralServer = MobileServerController.getInstance()
- .getCentralServer();
- return executeServerRequest(centralServer.getMobileServerKey(), serviceName, parameters);
+ return executeServerRequest(MobileServerController.getInstance().getCentralServerKey(),
+ serviceName, parameters);
}
/**
@@ -326,9 +325,8 @@
}
// if offline-->online, then check if we can go online
- final MobileServerDefinition thisServer = MobileServerController.getInstance()
- .getThisServerDefinition();
- if (thisServer.getStatus().equals(MobileServerState.OFFLINE.getValue())) {
+ if (MobileServerController.getInstance().getThisMobileServerState()
+ .equals(MobileServerState.OFFLINE.getValue())) {
// check if all state changers are online
final OBQuery<MobileServerDefinition> offlineServers = OBDal.getInstance().createQuery(
MobileServerDefinition.class,
@@ -339,7 +337,7 @@
+ MobileServerUtils.MAIN_SERVER + "' and "
+ MobileServerDefinition.PROPERTY_TRIGGERSTATE + "=true and "
+ MobileServerDefinition.PROPERTY_MOBILESERVERKEY + "!='"
- + thisServer.getMobileServerKey() + "' and "
+ + MobileServerController.getInstance().getThisMobileServerKey() + "' and "
+ MobileServerDefinition.PROPERTY_STATUS + "!='"
+ MobileServerState.ONLINE.getValue() + "'");
offlineServers.setFilterOnReadableClients(false);
diff --git a/src/org/openbravo/mobile/core/servercontroller/MobileServerStatusInformation.java b/src/org/openbravo/mobile/core/servercontroller/MobileServerStatusInformation.java
--- a/src/org/openbravo/mobile/core/servercontroller/MobileServerStatusInformation.java
+++ b/src/org/openbravo/mobile/core/servercontroller/MobileServerStatusInformation.java
@@ -39,8 +39,7 @@
final Session session = OBDal.getInstance().getSession();
final String hql = "select status from OBMOBC_SERVER_DEFINITION where id=:serverId";
final Query query = session.createQuery(hql);
- query.setString("serverId", MobileServerController.getInstance().getThisServerDefinition()
- .getId());
+ query.setString("serverId", MobileServerController.getInstance().getThisServerDefinitionId());
final Object qryResult = query.uniqueResult();
if (qryResult != null) {
w.write("\"serverStatus\": \"" + qryResult + "\"");
diff --git a/src/org/openbravo/mobile/core/servercontroller/ServerStateBackground.java b/src/org/openbravo/mobile/core/servercontroller/ServerStateBackground.java
--- a/src/org/openbravo/mobile/core/servercontroller/ServerStateBackground.java
+++ b/src/org/openbravo/mobile/core/servercontroller/ServerStateBackground.java
@@ -152,8 +152,8 @@
// transition depending on the number of online/offline servers
// read the server def again as the checks above can have taken some time
thisServerDef = MobileServerController.getInstance().getThisServerDefinition();
- final MobileServerState state = MobileServerController.getInstance().getMobileServerState(
- thisServerDef);
+ final MobileServerState state = MobileServerController.getInstance()
+ .getThisMobileServerState();
String logAction = "";
if (cntOfflineServers > 0) {
// offline servers, let's go offline
|