001: /****************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2001, ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ****************************************************************************/package net.sourceforge.cruisecontrol.distributed.util;
037:
038: import java.rmi.RemoteException;
039:
040: import net.jini.core.lookup.ServiceItem;
041: import net.jini.core.lookup.ServiceMatches;
042: import net.jini.core.lookup.ServiceRegistrar;
043: import net.jini.core.lookup.ServiceTemplate;
044: import net.sourceforge.cruisecontrol.distributed.core.MulticastDiscovery;
045:
046: import org.apache.log4j.Logger;
047:
048: /**
049: * This utility class finds Jini registrars on the local subnet and itemizes
050: * each of their registered services. It is needed solely for testing purposes.
051: *
052: * Run the main() method to see the local Jini services.
053: */
054: public final class JiniLookUpUtility {
055:
056: private static final Logger LOG = Logger
057: .getLogger(JiniLookUpUtility.class);
058:
059: private JiniLookUpUtility() {
060: MulticastDiscovery.begin();
061: final String waitMessage = "Waiting 5 seconds for registrars to report in...";
062: System.out.println(waitMessage);
063: LOG.info(waitMessage);
064: try {
065: try {
066: Thread.sleep(5000);
067: } catch (InterruptedException e1) {
068: // ignore
069: }
070: final ServiceRegistrar[] registrars = MulticastDiscovery
071: .getRegistrars();
072: System.out.println("\nFound " + registrars.length
073: + " Lookup Services.");
074: for (int x = 0; x < registrars.length; x++) {
075: final ServiceRegistrar registrar = registrars[x];
076: final String registrarInfo = "Registrar: "
077: + registrar.getServiceID();
078: System.out.println(registrarInfo);
079: LOG.debug(registrarInfo);
080: final ServiceTemplate template = new ServiceTemplate(
081: null, null, null);
082: final ServiceMatches matches = registrar.lookup(
083: template, Integer.MAX_VALUE);
084: final ServiceItem[] items = matches.items;
085: for (int i = 0; i < items.length; i++) {
086: final String serviceInfo = " Service: "
087: + items[i].service;
088: System.out.println(serviceInfo);
089: LOG.debug(serviceInfo);
090: }
091: }
092: } catch (RemoteException e) {
093: final String message = "Search failed due to an unexpected error";
094: LOG.error(message, e);
095: System.err.println(waitMessage + " - " + e.getMessage());
096: throw new RuntimeException(message, e);
097: }
098: }
099:
100: public static void main(final String[] args) {
101: new JiniLookUpUtility();
102: }
103: }
|