PageRenderTime 29ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/jade/src/jade/BootProfileImpl.java

https://bitbucket.org/jsadlo/sag_wedt
Java | 442 lines | 255 code | 59 blank | 128 comment | 79 complexity | b167ef22c2ab98ea33dff3a656e8571e MD5 | raw file
  1. /**
  2. *
  3. * JADE - Java Agent DEvelopment Framework is a framework to develop
  4. * multi-agent systems in compliance with the FIPA specifications.
  5. * Copyright (C) 2000 CSELT S.p.A.
  6. *
  7. * GNU Lesser General Public License
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation,
  12. * version 2.1 of the License.
  13. *
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 02111-1307, USA.
  24. *
  25. */
  26. package jade;
  27. //#APIDOC_EXCLUDE_FILE
  28. //#J2ME_EXCLUDE_FILE
  29. import java.net.InetAddress;
  30. import java.util.Enumeration; // J2ME CLDC OK
  31. import java.util.Vector; // J2ME CLDC OK
  32. import jade.util.leap.List;
  33. import jade.util.leap.ArrayList;
  34. import jade.util.leap.Iterator;
  35. import jade.util.leap.Properties;
  36. import jade.util.ExtendedProperties;
  37. import jade.util.PropertiesException;
  38. import jade.core.ProfileImpl;
  39. import jade.core.Profile;
  40. import jade.core.Specifier;
  41. /**
  42. * A profile implementation enhanced to support boot's
  43. * argument parsing. This class serves as the bridge between
  44. * boot properties and profile properties. It defines a
  45. * collection of property keys which correspond to boot
  46. * argument names. These are used to access the boot properties
  47. * from the argument properties. The class Profile defines
  48. * a similar collection of keys which are used to access profile
  49. * properties.
  50. * @author Dick Cowan - HP Labs
  51. * @version $Date: 2011-03-21 14:10:30 +0100(lun, 21 mar 2011) $ $Revision: 6395 $
  52. */
  53. public class BootProfileImpl extends ProfileImpl {
  54. public static final String ACLCODEC_KEY = "aclcodec";
  55. public static final String CONF_KEY = "conf";
  56. public static final String CONTAINER_KEY = "container";
  57. public static final String DUMP_KEY = "dump";
  58. public static final String GUI_KEY = "gui";
  59. public static final String HELP_KEY = "help";
  60. public static final String MTP_KEY = "mtp";
  61. public static final String NOMTP_KEY = "nomtp";
  62. public static final String NAME_KEY = "name";
  63. public static final String LOGIN_KEY = "auth";
  64. public static final String SMHOST_KEY = "smhost";
  65. public static final String SMPORT_KEY = "smport";
  66. public static final String VERSION_KEY = "version";
  67. public static final String NOMOBILITY_KEY = "nomobility";
  68. ExtendedProperties argProp = null;
  69. BootHelper helper = new BootHelper();
  70. /**
  71. * Construct default profile with empty argument properties
  72. */
  73. public BootProfileImpl() {
  74. // creates the default profile
  75. super(true);
  76. argProp = new ExtendedProperties();
  77. }
  78. /**
  79. * Construct profile with specified arguments
  80. * @param args Boot arguments
  81. */
  82. public BootProfileImpl(String[] args) throws PropertiesException {
  83. this();
  84. ExtendedProperties properties = new ExtendedProperties(args);
  85. bootProps = (Properties)properties.clone();
  86. setArgProperties(properties);
  87. }
  88. /**
  89. * Return the properties collection which resulted from the arguments.
  90. * This collection is used to create/modify the underlying profile's
  91. * properties.
  92. * @return ExtendedProperties The argument property collection.
  93. */
  94. public ExtendedProperties getArgProperties() {
  95. return argProp;
  96. }
  97. /**
  98. * Copy a collection of argument properties into the existing
  99. * argument properties and then into the profile properties.
  100. * When moving between the argument properties and profile
  101. * properties different keys are required.
  102. * @param source A collection of argument properties. The
  103. * keys to this collection are from the XXX_KEY strings
  104. * defined in this class.
  105. */
  106. public void setArgProperties(ExtendedProperties source) {
  107. argProp.copyProperties(source);
  108. String value = null;
  109. boolean flag = false;
  110. // Transfer argument properties into profile properties
  111. ExtendedProperties profileProp = (ExtendedProperties)getProperties();
  112. boolean isMain = true;
  113. if (argProp.getProperty(Profile.MAIN) != null) {
  114. isMain = fetchAndVerifyBoolean(Profile.MAIN);
  115. }
  116. else {
  117. isMain = !fetchAndVerifyBoolean(CONTAINER_KEY);
  118. }
  119. if (isMain) {
  120. profileProp.setProperty(Profile.MAIN, "true");
  121. }
  122. else {
  123. profileProp.setProperty(Profile.MAIN, "false");
  124. // Since the value is false, we cancel the default done in ProfileImpl's constructor
  125. setSpecifiers(Profile.MTPS, new ArrayList(0)); // remove default MTP
  126. }
  127. String sm = argProp.getProperty(LOCAL_SERVICE_MANAGER);
  128. if(sm != null) {
  129. profileProp.setProperty(Profile.LOCAL_SERVICE_MANAGER, sm);
  130. }
  131. /* PER IL DF
  132. value = argProp.getProperty(df.KBDF_MAX_RESULTS);
  133. if (value != null) {
  134. profileProp.setProperty(df.KBDF_MAX_RESULTS, value);
  135. }
  136. value = argProp.getProperty(df.DBDF_DRIVER);
  137. if (value != null) {
  138. profileProp.setProperty(df.DBDF_DRIVER, value);
  139. }
  140. value = argProp.getProperty(df.DBDF_URL);
  141. if (value != null) {
  142. profileProp.setProperty(df.DBDF_URL, value);
  143. }*/
  144. /*
  145. value = argProp.getProperty(PASSWD_KEY);
  146. if (value != null) {
  147. //profileProp.setProperty(Profile.PASSWD_FILE, value);
  148. }
  149. */
  150. value = argProp.getProperty(IMTP);
  151. if (value != null) {
  152. profileProp.setProperty(Profile.IMTP, value);
  153. }
  154. String host = argProp.getProperty(MAIN_HOST);
  155. if (host != null) {
  156. profileProp.setProperty(Profile.MAIN_HOST, host);
  157. } else {
  158. host = profileProp.getProperty(Profile.MAIN_HOST);
  159. if (host == null) {
  160. host = getDefaultNetworkName();
  161. profileProp.setProperty(Profile.MAIN_HOST, host);
  162. }
  163. }
  164. String port = argProp.getProperty(MAIN_PORT);
  165. if (port == null) {
  166. // Default for a sole main container: use the local port, or
  167. // the default port if also the local port is null.
  168. if(isMasterMain()) {
  169. port = argProp.getProperty(LOCAL_PORT);
  170. }
  171. if(port == null) {
  172. // All other cases: use the default port.
  173. port = Integer.toString(DEFAULT_PORT);
  174. }
  175. }
  176. profileProp.setProperty(Profile.MAIN_PORT, port);
  177. String localHost = argProp.getProperty(LOCAL_HOST);
  178. if(localHost == null) {
  179. // Default for a sole main container: use the MAIN_HOST property
  180. if(isMasterMain()) {
  181. localHost = host;
  182. }
  183. else {
  184. // Default for a peripheral container or an added main container: use the local host
  185. localHost = getDefaultNetworkName();
  186. }
  187. }
  188. profileProp.setProperty(Profile.LOCAL_HOST, localHost);
  189. String localPort = argProp.getProperty(LOCAL_PORT);
  190. if(localPort == null) {
  191. // Default for a sole main container: use the MAIN_PORT property
  192. if(isMasterMain()) {
  193. localPort = port;
  194. }
  195. else {
  196. // Default for a peripheral container or an added main container: use the default port
  197. localPort = Integer.toString(DEFAULT_PORT);
  198. }
  199. }
  200. profileProp.setProperty(Profile.LOCAL_PORT, localPort);
  201. value = argProp.getProperty(REMOTE_SERVICE_MANAGER_ADDRESSES);
  202. if (value != null) {
  203. try {
  204. Vector v = Specifier.parseSpecifierList(value);
  205. // Convert the Vector into a List
  206. List l = new ArrayList(v.size());
  207. Enumeration e = v.elements();
  208. while (e.hasMoreElements()) {
  209. l.add(e.nextElement());
  210. }
  211. setSpecifiers(Profile.REMOTE_SERVICE_MANAGER_ADDRESSES, l);
  212. }
  213. catch(Exception e) {
  214. e.printStackTrace();
  215. }
  216. }
  217. value = argProp.getProperty(NAME_KEY);
  218. if (value != null) {
  219. profileProp.setProperty(Profile.PLATFORM_ID, value);
  220. }
  221. value = argProp.getProperty(LOGIN_KEY);
  222. if (value != null) {
  223. profileProp.setProperty(Profile.USERAUTH_KEY, value);
  224. }
  225. value = argProp.getProperty(MTP_KEY);
  226. if (value != null) {
  227. setSpecifiers(Profile.MTPS, parseSpecifiers(value));
  228. }
  229. //NOMTP
  230. flag = fetchAndVerifyBoolean(NOMTP_KEY);
  231. if (flag) {
  232. // Since the value was set to true, cancel the MTP settings
  233. setSpecifiers(Profile.MTPS, new ArrayList(0));
  234. }
  235. value = argProp.getProperty(ACLCODEC_KEY);
  236. if (value != null) {
  237. setSpecifiers(Profile.ACLCODECS, parseSpecifiers(value));
  238. }
  239. // Get agent list (if any)
  240. value = argProp.getProperty(AGENTS);
  241. flag = fetchAndVerifyBoolean(GUI_KEY);
  242. if (flag) {
  243. // need to run RMA agent
  244. if (value != null) {
  245. value = "RMA:jade.tools.rma.rma " + value; // put before other agents
  246. } else {
  247. value = "RMA:jade.tools.rma.rma"; // only one
  248. }
  249. }
  250. if (value != null) {
  251. Vector agentVector = helper.T2(value, false);
  252. List agents = new ArrayList();
  253. for (Enumeration e = helper.getCommandLineAgentSpecifiers(agentVector);
  254. e.hasMoreElements(); ) {
  255. agents.add((Specifier) e.nextElement());
  256. }
  257. setSpecifiers(Profile.AGENTS, agents);
  258. }
  259. // Get service list (if any)
  260. value = argProp.getProperty(SERVICES);
  261. if(value == null) {
  262. // Remove mobility service from the list if '-nomobility' was specified
  263. flag = fetchAndVerifyBoolean(NOMOBILITY_KEY);
  264. if (flag) {
  265. value = DEFAULT_SERVICES_NOMOBILITY;
  266. }
  267. else {
  268. value = DEFAULT_SERVICES;
  269. }
  270. }
  271. setSpecifiers(Profile.SERVICES, parseSpecifiers(value));
  272. // finally, copy into profileProp all the properties that
  273. // were present in argProp AND were not already present in profileProp.
  274. // The fact that we do not copy those properties that were already
  275. // present in profileProp is a sanity check to avoid modification
  276. // of properties that were set in the code above
  277. for (Enumeration e=argProp.keys(); e.hasMoreElements(); ) {
  278. String key = (String)e.nextElement();
  279. if (getParameter(key,null) == null)
  280. setParameter(key, argProp.get(key).toString());
  281. }
  282. // The following is for debugging only. Probably should not document the "dumpProfile" attribute.
  283. // Note All the jade.util.leap.ArrayList structures will only print their type unless a
  284. // toString() method were added to them.
  285. if (argProp.getBooleanProperty("dumpProfile", false)) {
  286. ArrayList aList = new ArrayList();
  287. System.out.println("---------- Jade Boot profile property values ----------");
  288. for (Enumeration e = profileProp.sortedKeys(); e.hasMoreElements(); ) {
  289. String key = (String) e.nextElement();
  290. Object o = profileProp.get(key);
  291. if (o.getClass().isAssignableFrom(aList.getClass())) {
  292. System.out.print(key + "=");
  293. ArrayList al = (ArrayList)o;
  294. Iterator itor = al.iterator();
  295. if (!itor.hasNext()) {
  296. System.out.println("<empty>");
  297. } else {
  298. StringBuffer sb = new StringBuffer();
  299. while (itor.hasNext()) {
  300. sb.append(itor.next());
  301. if (itor.hasNext()) {
  302. sb.append(" ");
  303. }
  304. }
  305. System.out.println(sb.toString());
  306. }
  307. } else {
  308. System.out.println(key + "=" + profileProp.getProperty(key));
  309. }
  310. }
  311. System.out.println("-------------------------------------------------------");
  312. }
  313. }
  314. /**
  315. * Fetch and verify a boolean attribute.
  316. * @param aKey The property key to check.
  317. * @return True or false depending on the attributes setting. False if attribute doesn't exist.
  318. * @throws PropertiesException if there is a value but its not either "true" or "false".
  319. */
  320. protected boolean fetchAndVerifyBoolean(String aKey) throws PropertiesException {
  321. String value = argProp.getProperty(aKey);
  322. if (value != null) {
  323. if (value.equalsIgnoreCase("true")) {
  324. return true;
  325. }
  326. if (value.equalsIgnoreCase("false")) {
  327. return false;
  328. }
  329. throw new PropertiesException("The value of the attribute " + aKey + " must be either true or false.");
  330. }
  331. return false;
  332. }
  333. private static final String ARGUMENT_SEPARATOR = ";";
  334. /**
  335. * Parse a String reading for a set of
  336. * <code>parameter(arg)</code>
  337. * each delimited by a <code>;</code> and no space in between.
  338. * <p>
  339. * For instance
  340. * <code>jade.mtp.iiop(50);http.mtp.http(8080)</code> is a valid
  341. * string, while <code>jade.mtp.iiop(50 80);http.mtp.http(8080)</code>
  342. * is not valid
  343. * For each object specifier, a new java object <code>Specifier</code>
  344. * is added to the passed <code>out</code> List parameter.
  345. */
  346. public List parseSpecifiers(String str) throws PropertiesException {
  347. List result = new ArrayList();
  348. // Cursor on the given string: marks the parser position
  349. int cursor = 0;
  350. while (cursor < str.length()) {
  351. int commaPos = str.indexOf(ARGUMENT_SEPARATOR, cursor);
  352. if (commaPos == -1) {
  353. commaPos = str.length();
  354. }
  355. String arg = str.substring(cursor, commaPos);
  356. int openBracketPos = arg.indexOf('(');
  357. int closedBracketPos = arg.indexOf(')');
  358. Specifier s = new Specifier();
  359. if ((openBracketPos == -1) && (closedBracketPos == -1)) {
  360. // No brackets: no argument
  361. s.setClassName(arg);
  362. } else {
  363. // An open bracket, then something, then a closed bracket:
  364. // the class name is before the open bracket, and the
  365. // argument is between brackets.
  366. if ((openBracketPos != -1) && (closedBracketPos != -1)
  367. && (openBracketPos < closedBracketPos)) {
  368. s.setClassName(arg.substring(0, openBracketPos));
  369. Object a[] = new Object[1];
  370. a[0] = arg.substring(openBracketPos + 1,
  371. closedBracketPos);
  372. s.setArgs(a);
  373. } else {
  374. throw new PropertiesException(
  375. "Ill-formed specifier: mismatched parentheses.");
  376. }
  377. }
  378. cursor = commaPos + 1;
  379. result.add(s);
  380. } // while (cursor)
  381. return result;
  382. }
  383. }