View Javadoc

    1   /*
    2    * @(#) ContextUtil.java Oct 26, 2003
    3    *
    4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
    5    * the Netherlands All rights reserved.
    6    *
    7    * This software is proprietary information of Delft University of Technology
    8    * The code is published under the General Public License
    9    */
    10  package nl.tudelft.simulation.dsol.context;
    11
    12  import javax.naming.Context;
    13  import javax.naming.InitialContext;
    14
    15  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
    16  import nl.tudelft.simulation.logger.Logger;
    17
    18  /***
    19   * ContextUtil provides utility methods to resolve and bind objects to the
    20   * context based on their experiment dependent path. <br>
    21   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
    22   * University of Technology </a>, the Netherlands. <br>
    23   * See for project information <a
    24   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
    25   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
    26   * License (GPL) </a>, no warranty <br>
    27   *
    28   * @version 1.0 26.10.2003 <br>
    29   * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
    30   *         Jacobs </a>
    31   */
    32  public final class ContextUtil extends
    33  		nl.tudelft.simulation.naming.context.ContextUtil
    34  {
    35  	/***
    36  	 * constructs a new ContextUtil
    37  	 */
    38  	private ContextUtil()
    39  	{
    40  		super();
    41  	}
    42
    43  	/***
    44  	 * creates a subContext in the context
    45  	 *
    46  	 * @param simulator the simulator
    47  	 * @param subContext the context to create
    48  	 */
    49  	public static void createSubContext(final SimulatorInterface simulator,
    50  			final String subContext)
    51  	{
    52  		ContextUtil.createSubContext(simulator, null, subContext);
    53  	}
    54
    55  	/***
    56  	 * creates a subContext in the context
    57  	 *
    58  	 * @param simulator the simulator
    59  	 * @param context the context
    60  	 * @param subContext the context to create
    61  	 */
    62  	public static void createSubContext(final SimulatorInterface simulator,
    63  			final String context, final String subContext)
    64  	{
    65  		try
    66  		{
    67  			String name = simulator.getReplication().getRunControl()
    68  					.getTreatment().getExperiment().getRun()
    69  					+ "/treatment("
    70  					+ simulator.getReplication().getRunControl().getTreatment()
    71  							.getNumber()
    72  					+ ")/replication("
    73  					+ simulator.getReplication().getNumber() + ")";
    74  			if (context != null)
    75  			{
    76  				name = name + context;
    77  			}
    78  			((Context) new InitialContext().lookup(name))
    79  					.createSubcontext(subContext);
    80  		} catch (Exception exception)
    81  		{
    82  			Logger.warning(ContextUtil.class, "createSubContext", exception);
    83  		}
    84  	}
    85
    86  	/***
    87  	 * binds an object to the context
    88  	 *
    89  	 * @param simulator the simulator
    90  	 * @param object the object
    91  	 */
    92  	public static void bindToContext(final SimulatorInterface simulator,
    93  			final Object object)
    94  	{
    95  		ContextUtil.bindToContext(simulator, null, object);
    96  	}
    97
    98  	/***
    99  	 * binds an object to the context
    100 	 *
    101 	 * @param simulator the simulator
    102 	 * @param context the context in this tree
    103 	 * @param object the object
    104 	 */
    105 	public static void bindToContext(final SimulatorInterface simulator,
    106 			final String context, final Object object)
    107 	{
    108 		try
    109 		{
    110 			String name = simulator.getReplication().getRunControl()
    111 					.getTreatment().getExperiment().getRun()
    112 					+ "/treatment("
    113 					+ simulator.getReplication().getRunControl().getTreatment()
    114 							.getNumber()
    115 					+ ")/replication("
    116 					+ simulator.getReplication().getNumber() + ")";
    117 			if (context != null)
    118 			{
    119 				name = name + context;
    120 			}
    121 			((Context) new InitialContext().lookup(name)).bind(object
    122 					.getClass().getName().substring(
    123 							object.getClass().getName().lastIndexOf("."))
    124 					+ "#" + object.toString(), object);
    125 		} catch (Exception exception)
    126 		{
    127 			Logger.warning(ContextUtil.class, "bindToContext", exception);
    128 		}
    129 	}
    130 }