001: /*
002: Copyright (C) 2004 David Bucciarelli (davibu@interfree.it)
003:
004: This program is free software; you can redistribute it and/or
005: modify it under the terms of the GNU General Public License
006: as published by the Free Software Foundation; either version 2
007: of the License, or (at your option) any later version.
008:
009: This program is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: GNU General Public License for more details.
013:
014: You should have received a copy of the GNU General Public License
015: along with this program; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: */
018:
019: package org.homedns.dade.jcgrid;
020:
021: import java.io.*;
022: import java.util.*;
023:
024: import org.homedns.dade.jcgrid.server.*;
025:
026: public abstract class GridNodeGenericConfig extends GridNodeConfig
027: implements Cloneable {
028: private static Random rndGenerator = new Random();
029:
030: private static String generateSessionID() {
031: final String[] alphabet = { "A", "B", "C", "D", "E", "F", "G",
032: "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
033: "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c",
034: "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
035: "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y",
036: "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
037:
038: StringBuffer sb = new StringBuffer();
039: synchronized (rndGenerator) {
040: for (int i = 0; i < 8; i++)
041: sb.append(alphabet[rndGenerator
042: .nextInt(alphabet.length)]);
043: }
044:
045: return sb.toString();
046: }
047:
048: private String sessionName;
049:
050: public GridNodeGenericConfig(Properties prop) {
051: super (prop);
052:
053: sessionName = prop
054: .getProperty("grid.node.session.name", "none");
055: }
056:
057: public GridNodeGenericConfig(String type) {
058: super (type);
059:
060: sessionName = "none";
061: }
062:
063: public Object clone() {
064: GridNodeGenericConfig cfg = (GridNodeGenericConfig) super
065: .clone();
066:
067: cfg.sessionName = sessionName;
068:
069: return cfg;
070: }
071:
072: public Properties toProperties() {
073: Properties prop = super .toProperties();
074:
075: prop.put("grid.node.session.name", sessionName);
076:
077: return prop;
078: }
079:
080: public void autoSessioName() {
081: String s = System.getProperty("user.name", "noname") + "_"
082: + this .generateSessionID();
083: sessionName = s.replaceAll("[^a-zA-Z0-9_]+", "_");
084: }
085:
086: //------------------------------- Get & Set --------------------------------
087:
088: public String getSessionName() {
089: return sessionName;
090: }
091:
092: public void setSessionName(String name) throws Exception {
093: // If you change the regexp check also the server side code
094:
095: if (!name.matches("[a-zA-Z0-9_]+"))
096: throw new Exception("Invalid session name");
097:
098: sessionName = name;
099: }
100: }
|