package art.servers;
|
|
import art.library.model.devices.Device;
|
import art.library.model.devices.user.UserPermission;
|
import art.library.model.transactions.traces.Note;
|
import art.library.model.transactions.traces.Trace;
|
import art.library.utils.licence.Licence;
|
import art.library.utils.mail.MailMessage;
|
import art.library.utils.mail.SMTP;
|
import art.library.utils.mail.SMTPConfiguration;
|
import art.library.utils.resources.Resources;
|
import art.library.utils.synchro.Mutex;
|
import art.servers.configuration.Configuration;
|
import art.servers.controller.Controller;
|
import art.servers.controller.ControllerDatabase;
|
import art.servers.controller.ControllerDevice;
|
import art.servers.controller.ControllerListener;
|
import art.servers.controller.ControllerListenerDebug;
|
import art.servers.controller.ControllerListenerHttp;
|
import art.servers.controller.ControllerListenerHttps;
|
import art.servers.controller.ControllerNtpHttp;
|
import art.servers.controller.ControllerProcessInformation;
|
import art.servers.controller.ControllerStatus;
|
import art.servers.controller.ControllerTransactions;
|
import art.servers.controller.ControllerUserPermissions;
|
import art.servers.controller.FactoryController;
|
import art.servers.gui.ArticWindow;
|
import art.servers.types.HttpAuthentication;
|
import java.io.PrintWriter;
|
import java.io.StringWriter;
|
import java.net.InetAddress;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Timer;
|
import java.util.TimerTask;
|
|
public class Shared
|
{
|
private static String applicationCode = null;
|
private static String applicationName = null;
|
|
public static boolean debug = false;
|
public static boolean reloadDevices = false;
|
public static boolean restoreDevices = false;
|
|
public static Configuration configuration = null;
|
public static Model model = null;
|
public static FactoryController factory = null;
|
public static ControllerStatus controllerStatus = null;
|
public static ControllerDatabase controllerDatabase = null;
|
public static ControllerListener controllerListener = null;
|
public static ControllerTransactions controllerTransactions = null;
|
public static ControllerListenerHttp controllerListenerHttp = null;
|
public static ControllerListenerHttps controllerListenerHttps = null;
|
public static ControllerUserPermissions controllerUserPermissions = null;
|
public static ControllerProcessInformation controllerProcessInformation = null;
|
public static ControllerNtpHttp controllerNtpHttp = null;
|
|
public static List<Controller> lcontroller = new ArrayList<Controller>();
|
private static HashMap<String, UserPermission> usersPermissions = new HashMap<String, UserPermission>();
|
public static ArticWindow window = null;
|
public static ControllerListenerDebug controllerListenerDEBUG = null;
|
private static Mutex mutexUserPermissions = new Mutex();
|
|
|
public static int RESULT_OK = 0;
|
public static int ERROR_WRONG_PASSWORD = 1;
|
public static int ERROR_FIRST_ACCESS = 2;
|
public static int ERROR_BLOCKED_USER = 3;
|
public static int ERROR_BLOCKED_ADDRESS = 4;
|
|
|
public static void setApplicationCode(String code)
|
{
|
Shared.applicationCode = code;
|
}
|
|
public static void setApplicationName(String name)
|
{
|
Shared.applicationName = name;
|
}
|
|
public static String getApplicationName()
|
{
|
return Shared.applicationName;
|
}
|
|
public static String getApplicationCode()
|
{
|
return Shared.applicationCode;
|
}
|
|
public static String getLanguage()
|
{
|
return configuration.general.language;
|
}
|
|
public static void setLanguage(String language)
|
{
|
try
|
{
|
configuration.setLanguage(language);
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
public static String getMessage(String language, String identifier)
|
{
|
return configuration.getMessage(language, identifier);
|
}
|
|
|
|
|
public static UserPermission getUserPermission(String username)
|
{
|
try
|
{
|
mutexUserPermissions.lockRead();
|
return(usersPermissions.get(username));
|
}
|
catch (Exception e)
|
{
|
return(null);
|
}
|
finally
|
{
|
mutexUserPermissions.releaseRead();
|
}
|
}
|
|
|
public static void setUserPermission(String username, UserPermission permission)
|
{
|
try
|
{
|
mutexUserPermissions.lockWrite();
|
usersPermissions.put(username, permission);
|
}
|
catch (Exception e)
|
{
|
|
}
|
finally
|
{
|
mutexUserPermissions.releaseWrite();
|
}
|
}
|
|
|
|
public static String getMessage(String identifier)
|
{
|
return configuration != null ? configuration.getMessage(identifier) : identifier;
|
}
|
|
|
public static <T>T getDeviceController(Device device) throws ServerException
|
{
|
return getDeviceController(device.getIdentifier());
|
}
|
|
|
public static <T>T getDeviceController(String identifier) throws ServerException
|
{
|
try
|
{
|
// Acceso a la lista sin mutex, en algun caso podria haber colisión si se elinina
|
// algun dispositivo y se quita de la lista mientras estamos leyendo
|
|
for (Controller controller : lcontroller)
|
{
|
if (controller instanceof ControllerDevice)
|
{
|
ControllerDevice controllerDevice = (ControllerDevice)controller;
|
|
if (((Device)controllerDevice.getDevice()).getIdentifier().equals(identifier))
|
{
|
return (T)controller;
|
}
|
}
|
}
|
}
|
catch (Exception e)
|
{
|
}
|
|
throw new ServerException("Device controller does not exists");
|
}
|
|
|
|
public static <T>T getDeviceController(String classname, int number) throws ServerException
|
{
|
try
|
{
|
// Acceso a la lista sin mutex, en algun caso podria haber colisión si se elinina
|
// algun dispositivo y se quita de la lista mientras estamos leyendo
|
|
for (Controller controller : lcontroller)
|
{
|
if (controller instanceof ControllerDevice)
|
{
|
ControllerDevice controllerDevice = (ControllerDevice)controller;
|
Device device = ((Device)controllerDevice.getDevice());
|
|
if ((device.getClassName().equalsIgnoreCase(classname)) && (device.getDeviceInformation().number == number))
|
{
|
return (T)controller;
|
}
|
}
|
}
|
}
|
catch (Exception e)
|
{
|
}
|
|
throw new ServerException("Device controller does not exists");
|
}
|
|
|
|
|
public static List<ControllerDevice> getDeviceControllers() throws ServerException
|
{
|
List<ControllerDevice> result = new ArrayList<ControllerDevice>();
|
|
try
|
{
|
// Acceso a la lista sin mutex, en algun caso podria haber colisión si se elinina
|
// algun dispositivo y se quita de la lista mientras estamos leyendo
|
|
for (Controller controller : lcontroller)
|
{
|
if (controller instanceof ControllerDevice)
|
{
|
result.add((ControllerDevice)controller);
|
}
|
}
|
}
|
catch (Exception e)
|
{
|
}
|
|
return(result);
|
}
|
|
|
|
|
public static boolean existsDeviceController(Device device)
|
{
|
return existsDeviceController(device.getIdentifier());
|
}
|
|
|
|
public static boolean existsDeviceController(String identifier)
|
{
|
try
|
{
|
for (Controller controller : lcontroller)
|
{
|
if (controller instanceof ControllerDevice)
|
{
|
ControllerDevice controllerDevice = (ControllerDevice)controller;
|
|
if (((Device)controllerDevice.getDevice()).getIdentifier().equals(identifier))
|
{
|
return true;
|
}
|
}
|
}
|
}
|
catch (Exception e)
|
{
|
}
|
|
return false;
|
}
|
|
|
|
|
|
|
public static boolean mailNotificacion(Device device, MailMessage message)
|
{
|
boolean result = true;
|
|
for (SMTPConfiguration configuration : Shared.configuration.lsmtp)
|
{
|
try
|
{
|
configuration.smtpPassword = Licence.decrypt(configuration.smtpPassword);
|
}
|
catch (Exception e)
|
{
|
}
|
|
try
|
{
|
try
|
{
|
SMTP smtp = new SMTP(configuration);
|
smtp.send(message);
|
}
|
catch (Exception exception)
|
{
|
result = false;
|
}
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
return result;
|
}
|
|
|
|
|
public static boolean isServerEnabled()
|
{
|
return true;
|
}
|
|
|
|
public static String version()
|
{
|
String message = "";
|
|
try
|
{
|
String data = new String(Resources.getResourceData("version.properties"));
|
data = data.replaceAll("(?m)^\\s*\\r?\\n|\\r?\\n\\s*(?!.*\\r?\\n)", "");
|
String[] lines = data.split("\\r?\\n");
|
message = message + (lines[2].replace("major=", "") + "." + lines[3].replace("minor=", "") + "." + lines[4].replace("revision=", "") + " " + lines[5].replace("=", " ") + ", " + lines[1].replace("date=", ""));
|
}
|
catch (Exception e)
|
{
|
}
|
|
return message;
|
}
|
|
|
|
public static void printVersion(String applicationName, String fileName)
|
{
|
try
|
{
|
String data = new String(Resources.getResourceData(fileName));
|
data = data.replaceAll("(?m)^\\s*\\r?\\n|\\r?\\n\\s*(?!.*\\r?\\n)", "");
|
String[] lines = data.split("\\r?\\n");
|
Shared.println(applicationName, "Version: " + (lines[2].replace("major=", "") + "." + lines[3].replace("minor=", "") + "." + lines[4].replace("revision=", "") + " " + lines[5].replace("=", " ") + ", " + lines[1].replace("date=", "")));
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
|
|
public static Trace getTrace()
|
{
|
Trace trace = new Trace();
|
trace.timestamp = System.currentTimeMillis();
|
trace.username = Shared.getApplicationName();
|
trace.application = Shared.getApplicationName();
|
|
try
|
{
|
trace.sourceComputer = InetAddress.getLocalHost().getHostName();
|
trace.destinationComputer = trace.sourceComputer;
|
}
|
catch (Exception e)
|
{
|
}
|
|
return trace;
|
}
|
|
|
|
|
|
|
|
public static void exit()
|
{
|
for (Controller controller : Shared.lcontroller)
|
{
|
try { controller.close(); } catch (Exception e) { }
|
}
|
|
Timer timer = new Timer();
|
timer.schedule(new TimerTask()
|
{
|
public void run ()
|
{
|
// Let some time to finish current operations
|
System.exit(0);
|
}
|
}, 5000);
|
}
|
|
|
|
public static boolean getPropertyValueBoolean(String key)
|
{
|
try
|
{
|
return(Boolean.parseBoolean(System.getProperty(key)));
|
}
|
catch (Exception exception)
|
{
|
|
}
|
|
return(false);
|
}
|
|
|
|
public static String getPropertyValueString(String key)
|
{
|
try
|
{
|
return(System.getProperty(key));
|
}
|
catch (Exception exception)
|
{
|
|
}
|
|
return(null);
|
}
|
|
|
|
|
|
// <editor-fold defaultstate="collapsed" desc="prints">
|
|
|
public static void println(Trace trace, boolean save)
|
{
|
try
|
{
|
controllerListenerDEBUG.println(trace, save);
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
|
public static void println(String service, String message)
|
{
|
try
|
{
|
controllerListenerDEBUG.println(new Note(Trace.TRACE_INFORMATION, service + " | " + message), false);
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
public static void println(String service, Exception exception)
|
{
|
try
|
{
|
controllerListenerDEBUG.println(new Note(Trace.TRACE_ERROR, service + " | " + exception.toString()), false);
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
|
|
public static void printerr(String service, String message)
|
{
|
try
|
{
|
controllerListenerDEBUG.println(new Note(Trace.TRACE_ERROR, service + " | " + message), false);
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
|
public static void printstack(String service, Exception exception)
|
{
|
try
|
{
|
StringWriter sw = new StringWriter();
|
exception.printStackTrace(new PrintWriter(sw));
|
controllerListenerDEBUG.println(new Note(Trace.TRACE_ERROR, service + " | " + sw.toString()), false);
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
|
|
|
public static void printwarning(String service, String message)
|
{
|
try
|
{
|
controllerListenerDEBUG.println(new Note(Trace.TRACE_WARNING, service + " | " + message), false);
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
// </editor-fold>
|
|
|
|
// <editor-fold defaultstate="collapsed" desc="traces">
|
|
public static Trace traceInformation(String service, String action)
|
{
|
Trace trace = Trace.getTraceInformation(Shared.getApplicationName(), service, Shared.getMessage(action), Shared.getMessage("Success"));
|
println(trace, true);
|
return trace;
|
}
|
|
|
public static Trace traceInformation(String service, String action, String resource)
|
{
|
Trace trace = Trace.getTraceInformation(Shared.getApplicationName(), service, Shared.getMessage(action), Shared.getMessage("Success"));
|
trace.resource = resource;
|
println(trace, true);
|
return trace;
|
}
|
|
|
public static Trace traceInformation(String service, String action, HttpAuthentication authentication, String language)
|
{
|
Trace trace = Trace.getTraceInformation(Shared.getApplicationName(), service, Shared.getMessage(language, action), Shared.getMessage(language, "Success"));
|
|
if (authentication != null)
|
{
|
trace.username = authentication.username;
|
trace.sourceComputer = authentication.address;
|
}
|
|
println(trace, true);
|
return trace;
|
}
|
|
|
public static Trace traceInformation(String service, String action, String resource, HttpAuthentication authentication, String language)
|
{
|
Trace trace = Trace.getTraceInformation(Shared.getApplicationName(), service, Shared.getMessage(language, action), Shared.getMessage(language, "Success"));
|
|
if (authentication != null)
|
{
|
trace.username = authentication.username;
|
trace.sourceComputer = authentication.address;
|
}
|
|
trace.resource = resource;
|
println(trace, true);
|
return trace;
|
}
|
|
|
|
public static Trace traceError(String service, String action, Exception exception, HttpAuthentication authentication, String language)
|
{
|
Trace trace = Trace.getTraceError(Shared.getApplicationName(), service, Shared.getMessage(language, action), exception);
|
|
if (authentication != null)
|
{
|
trace.username = authentication.username;
|
trace.sourceComputer = authentication.address;
|
}
|
|
println(trace, true);
|
return trace;
|
}
|
|
|
|
public static Trace traceError(String service, String action, String resource, Exception exception, HttpAuthentication authentication, String language)
|
{
|
Trace trace = Trace.getTraceError(Shared.getApplicationName(), service, Shared.getMessage(language, action), exception);
|
|
if (authentication != null)
|
{
|
trace.username = authentication.username;
|
trace.sourceComputer = authentication.address;
|
}
|
|
trace.resource = resource;
|
println(trace, true);
|
return trace;
|
}
|
|
|
|
public static Trace traceError(String service, String action, Exception exception)
|
{
|
Trace trace = Trace.getTraceError(Shared.getApplicationName(), service, Shared.getMessage(action), exception);
|
println(trace, true);
|
return trace;
|
}
|
|
|
|
|
public static Trace traceError(String service, String action, String resource, Exception exception)
|
{
|
Trace trace = Trace.getTraceError(Shared.getApplicationName(), service, Shared.getMessage(action), exception);
|
trace.resource = resource;
|
println(trace, true);
|
return trace;
|
}
|
|
|
public static Trace traceWarning(String service, String action, String resource)
|
{
|
Trace trace = Trace.getTraceWarning(Shared.getApplicationName(), service, Shared.getMessage(action), Shared.getMessage("Success"));
|
trace.resource = Shared.getMessage(resource);
|
println(trace, true);
|
return trace;
|
}
|
|
|
|
public static Trace traceWarning(String service, String action, String resource, String result)
|
{
|
Trace trace = Trace.getTraceWarning(Shared.getApplicationName(), service, Shared.getMessage(action), Shared.getMessage(result));
|
trace.resource = Shared.getMessage(resource);
|
println(trace, true);
|
return trace;
|
}
|
|
// </editor-fold>
|
|
}
|