Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* ************************************************************************
  #
  #  DivConq
  #
  #  http://divconq.com/
  #
  #  Copyright:
  #    Copyright 2014 eTimeline, LLC. All rights reserved.
  #
 #  License:
 #    See the license.txt file in the project's top-level directory for details.
 #
 #  Authors:
 #    * Andy White
 #
 ************************************************************************ */
 package divconq.hub;
 
 import java.io.File;
 import java.util.List;
 
Within dcFramework all features are tied together through the Hub class. To get the hub going you need to give it access to some resources such as Schema and config. HubResources is the class that ties all the resources together and enables Hub to start. A typical application using dcFramework will start up something like this: HubResources resources = new HubResources("00101", true); resources.setDebugLevel(DebugLevel.Warn); OperationResult or = resources.init(); if (or.hasErrors()) { Logger.error("Unable to continue, hub resources not properly initialized"); return; } Hub.instance.start(resources); [TODO add link to Quick Start ] [TODO add link to Framework Architecture ] TODO consider an option where all config is loaded from AWS. Their description: You can use this data to build more generic AMIs that can be modified by configuration files supplied at launch time. For example, if you run web servers for various small businesses, they can all use the same AMI and retrieve their content from the Amazon S3 bucket you specify at launch. To add a new customer at any time, simply create a bucket for the customer, add their content, and launch your AMI.

Author(s):
Andy
 
 public class HubResources {
 	static public boolean isValidHubId(String id) {
 		if (StringUtil.isEmpty(id) || (id.length() != 5))
 			return false;
 		
 		for (int i = 0; i < 5; i++) 
 			if (!Character.isDigit(id.charAt(i))) 
 				return false;
 		
 		if ("00000".equals(id))
 			return false;
 		
 		return true;
 	}	
 	
 	protected String deployment = "dcFileServer";
 	protected String hubid = "00001";		// reserved for utilities and stand alones - 00000 reserved for system/core 
 	protected String team = "one";
 	protected String squad = "one";
 	protected HubMode mode = .;		// Gateway, Public (server), Private (server or utility)
 	
 	// post init
 	protected boolean initialized = false;
	protected boolean initsuccess = false;
	protected List<Stringpackages = new ArrayList<String>(); 
	protected List<Stringreversepackages = new ArrayList<String>(); 
	protected XElement config = null;
	// TODO
	//protected XElement fabric = null;
	protected Localization localization = null;
  	protected SchemaManager schemaman = null;

  
HubId is a 5 digit (zero padded) number that uniquely identifies this Hub (process) in the distributed network of Hubs (processes) in your Project (application).

Returns:
HubId
	public String getHubId() {
		return this.;
	}

HubId is a 5 digit (zero padded) number that uniquely identifies this Hub (process) in the distributed network of Hubs (processes) in your Project (application). You should only set the HubId once per run, Hubs are not designed to change Ids mid run.

Parameters:
v HubId
	public void setHubId(String v) {
		if (!HubResources.isValidHubId(v))
			throw new IllegalArgumentException("Hub id must be 5 digits, zero padded. Id 00000 is reserved.");
		OperationContext.setHubId(v);
		this. = v;
	}

false: this Hub is running on a private, fire-walled, network (company LAN) true: this Hub is running on a public network such as the Internet or in a DMZ

Returns:
true if is public facing
	public boolean isPublicFacing() {
		return (this. == .) || (this. == .);
	}

true: this Hub is not complete software, but is mostly support for network interchange

Returns:
true if is a gateway
	public boolean isGateway() {
		return (this. == .);
	}

Squad Id is used to group a number of Hubs together. A Squad is functionally separated from all other Squads and may operate independently (disaster recovery). A Squad may be active even while other Squads are, this is not a fail-over architecture but a active-active architecture. Squads may often be placed in separate data centers or spread across data centers and clouds (IaaS). A Squad has its own copy of the Project database, the database is replicated so that each Squad will eventually be consistent.

Returns:
Squad Id
	public String getSquadId() {
		return this.;
	}

Squad Id is used to group a number of Hubs together. A Squad is functionally separated from all other Squads and may operate independently (disaster recovery). A Squad may be active even while other Squads are, this is not a fail-over architecture but a active-active architecture. Squads may often be placed in separate data centers or spread across data centers and clouds (IaaS). A Squad has its own copy of the Project database, the database is replicated so that each Squad will eventually be consistent.

Parameters:
v Squad Id
	public void setSquadId(String v) {
		this. = v;
	}
	public String getTeamId() {
		return this.;
	}
	public void setTeamId(String v) {
		this. = v;
	}
	public HubMode getMode() {
		return this.;
	}

The global (default) Debug Level to use normally comes from the config file, before the config file is available Hub start-up uses the Debug Level given to this class.

Returns:
Debug Level used during start-up
		return this.;
	}

The global (default) Debug Level to use normally comes from the config file, before the config file is available Hub start-up uses the Debug Level given to this class.

Parameters:
v Debug Level used during start-up
	public void setDebugLevel(DebugLevel v) {
		this. = v;
	}

A list of package names in the order in which the packages are loaded. Each subsequent package overrides the previous. So, for example, the contents of the last package overrides any similar resource in all the previous packages.

Returns:
list of package names
		return this.;
	}

Schema holds the custom data types used by this Project.

Returns:
custom data type definitions
		return this.;
	}

Dictionary holds a list of Locales for which translations exists for this Project.

Returns:
Locales for this Project
		return this.;
	}

Config is an XML structure holding the master settings for this Project. Other configuration may come from the database.

Returns:
master Project settings
	public XElement getConfig() {
		return this.;
	}
	/* TODO
	public XElement getFabric() {
		return this.fabric;
	}
	*/
	public HubResources() {
	}

Manage the resources for this Hub by indicating which hub this is and whether to run in developer mode. See class, HubId and DevMode comments for details.

Parameters:
deployment project name for the servers (one or more squads)
squad the group of servers that forms a local operating group (one or more teams)
team the team of servers within the squad (one or more hubs)
hubid Hub Id
	public HubResources(String deploymentString squadString teamString hubid) {
		if (StringUtil.isNotEmpty(deployment))
			this. = deployment;
		if (StringUtil.isNotEmpty(squad))
			this. = squad;
		if (StringUtil.isNotEmpty(team))
			this. = team;
		if (StringUtil.isNotEmpty(hubid))
			this.setHubId(hubid);
	}

Initialize this object by loading the Schema, Dictionary, Config and Fabric. When in Dev Mode the resources are loaded from a local copy of the Repository instead of from the config directory. Developers will debug applications using the local Repository copy so that they can edit repository artifacts (schema, dictionary, resource files) in "native" repository structure.

Returns:
messages logged while initializing this object
	public OperationResult init() {
		// do not run init twice (not thread safe, should be called by main thread only)
		if (this.) {
			OperationResult or = new OperationResult(OperationContext.getHubContext());
			if (!this.)
				or.error(112, "Hub resources already loaded, but contained errors");
			return or;
		}				
		this. = true;
		// before starting we want to have a valid hub level task context
		// which requires a hub id
		OperationContext.setHubId(this.);
		// use the startup debug level until we init Logger settings
		or.info(0, "Loading hub resources");
		or.trace(0, "Loading shared config");
		File fshared = new File("./config/" + this. + "/_shared.xml");
		FuncResult<XElementxres = XmlReader.loadFile(fsharedfalse); 
		if (xres.hasErrors()) {
			or.copyMessages(xres);
			or.error(100, "Unable to load _shared.xml file, expected: " + fshared.getAbsolutePath());
			return or;
		}
		XElement cel = xres.getResult();
		for (XElement pack : cel.selectAll("Packages/Package")) {
			String n = pack.getAttribute("Name");
			this..add(n);
			this..add(0, n);
		}
		or.trace(0, "Packages loaded: " + this.);
		or.trace(0, "Loading config.xml file");
		// find the right config file
		File f = new File("./config/" + this. + "/" + this. + ".xml");
		if (!f.exists()) 
			f = new File("./config/" + this. + "/" + this. + ".xml");
		if (!f.exists()) 
			f = new File("./config/" + this. + "/" + this. + ".xml");
		if (!f.exists())
			f = new File("./config/" + this. + "/_config.xml");
		if (!f.exists()) {
			or.error(101, "Unable to find config.xml file, expected: " + f.getAbsolutePath());
			return or;
		}
		FuncResult<XElementxres2 = XmlReader.loadFile(ffalse);
		if (xres2.hasErrors()) {
			or.copyMessages(xres2);
			or.error(102, "Unable to load config file, expected: " + f.getAbsolutePath());
			return or;
		}
		this. = xres2.getResult(); 
		if (this..hasAttribute("HubId"))
			this.setHubId(this..getAttribute("HubId"));
		if (this..hasAttribute("Team"))
			this.setTeamId(this..getAttribute("Team"));
		if (this..hasAttribute("Squad"))
			this.setSquadId(this..getAttribute("Squad"));
		if (this..hasAttribute("Mode")) 
			this. = HubMode.valueOf(this..getAttribute("Mode"));
		or.trace(0, "Loaded config.xml file at: " + f.getAbsolutePath());
		or.trace(0, "Using project compiler to load schema and dictionary");
		this. = comp.getSchema(orthis.);
		if (or.hasErrors()) {
			or.exit(103, "Unable to load schema file(s)");
			return or;
		}
		or.trace(0, "Schema loaded");
		this. = comp.getDictionary(orthis.);
		if (or.hasErrors()) {
			or.exit(104, "Unable to load dictionary file(s)");
			return or;
		}
		or.trace(0, "Dictionary loaded");
		// TODO get fabric from ./project...
		// TODO load Clock Xml from http://169.254.169.254/latest/user-data
		// then over write
		//this.config.find("Clock").replace(parsed awssource);
		or.info(0, "Hub resources loaded");
		this. = true;
		return or;
	}

Scan through the local repository or config directory and reload the dictionary files. After this any translation used will have updates from the dictionary files. This method is really only useful in development mode and is not typically called by application code.

Returns:
messages logged while reloading dictionary
		OperationResult or = new OperationResult(OperationContext.getHubContext());
		or.trace(0, "Loading Dictionary");
		this. = comp.getDictionary(orthis.);
		if (or.hasErrors()) 
			or.exit(104, "Unable to load dictionary file(s)");
		else		
			or.trace("Dictionary loaded");
		return or;
	}

Get a reference to a package's resource file for this Project.

Parameters:
pkg name of the package the resource resides in
path of the file
Returns:
File reference if found, if not error messages in FuncResult
	public File getPackageResource(String pkgCommonPath path) {		// TODO support ZIP packagse
		for (String rcomponent : this.) {
			File f = new File("./packages/" + rcomponent + "/resource/" + pkg + path.toString());		// must be absolute path
			if (f.exists()) 
				return f;
		}
		return null;
	}
	// optimize common path with file path map...
	public Path getPackageWebFile(String pkgCommonPath path) {		// TODO support ZIP packagse
		if (path.hasFileExtension()) {
			Path fl = Paths.get("./packages/" + pkg + "/www" + path);		// must be absolute path
			if (Files.exists(fl)) 
				return fl;
			return null;
		}
		CommonPath parent = path.getParent();
		Path fld = (parent == null) ? Paths.get("./packages/" + pkg + "/www") :  Paths.get("./packages/" + pkg + "/www" + path.getParent());		// must be absolute path
		if (Files.notExists(fld)) 
			return null;
		// get the first file that looks like this name but with an extension - we want the extension even if it doesn't have to show 
		String name = path.getFileName() + ".";
		try (DirectoryStream<Pathstream = Files.newDirectoryStream(fld)) {
			for (Path p : stream) {
				String fname = p.getFileName().toString();
				if (fname.startsWith(name))
					return p;
			}
		catch (IOException x) {
		}
		return null;
	}

Get a reference to a library file (typically a JAR) for this Project.

Parameters:
filename name of the JAR, path relative to the lib/ folder
Returns:
File reference if found, if not error messages in FuncResult
	public FuncResult<FilegetLibrary(String filename) {
		FuncResult<Fileres = new FuncResult<File>(); 
		for (String rcomponent : this.) {
			File f = new File("./packages/" + rcomponent + "/lib/" + filename);
			if (f.exists()) {
				res.setResult(f);
				return res;
			}
		}
		res.errorTr(200, "./lib/" + filename);
		return res;
	}

Get a reference to a resource file specific for this Project.

Parameters:
filename name of the file, path relative to the resources/ folder in config
Returns:
Path reference if found, if not error messages in FuncResult
	public FuncResult<PathgetProjectResource(String filename) {
		FuncResult<Pathres = new FuncResult<>(); 
		Path f = Paths.get("./config/" + this. + "/resources/" + filename);
		if (Files.exists(f))  
			res.setResult(f);
		else
			res.errorTr(201, f.toString());
		return res;
	}
New to GrepCode? Check out our FAQ X