001: /* Copyright 2002 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.tools;
007:
008: import org.jasig.portal.ChannelRegistryStoreFactory;
009: import org.jasig.portal.ChannelType;
010: import org.jasig.portal.IChannelRegistryStore;
011: import org.jasig.portal.RDBMServices;
012:
013: /**
014: * <p>A tool for registering a new channel type with uPortal.
015: * Channel types are defined by a java class name and an associated
016: * channel publishing document. Once added with this tool, a channel
017: * type will be available to channel publishers for the production
018: * of new channel definitions based on the channel type.</p>
019: * <p>Usage: RegisterChannelType <class> <name> <description> <CPD URI></p>
020: * @author Ken Weiner, kweiner@unicon.net
021: * @version $Revision: 35664 $
022: */
023: public class RegisterChannelType {
024: protected static IChannelRegistryStore chanRegStore = null;
025:
026: public static void main(String[] args) {
027: RDBMServices.setGetDatasourceFromJndi(false); /*don't try jndi when not in web app */
028:
029: // Enforce that exactly 4 arguments are given: class, name, description, and URI
030: // and that no arguments are empty
031: if (args.length == 4) {
032: try {
033: msg(register(args[0].trim(), args[1].trim(), args[2]
034: .trim(), args[3].trim()));
035: } catch (IllegalArgumentException e) {
036: msg(e.getMessage());
037: printHelp();
038: } catch (Exception e) {
039: e.printStackTrace();
040: }
041: } else {
042: printHelp();
043: }
044: }
045:
046: /**
047: * Utility method for checking validity of type parameters.
048: * @param name
049: * @param value
050: * @throws IllegalArgumentException
051: */
052: private static void checkValidity(String name, String value)
053: throws IllegalArgumentException {
054: if (value == null || value.length() == 0)
055: throw new IllegalArgumentException(
056: "Parameter '"
057: + name
058: + "' must be specified when publishing a channel type.");
059: }
060:
061: /**
062: * Registers a new channel type for the given parameters or updates an
063: * existing type with the specified parameters if an existing type is found
064: * having the specified uri.
065: */
066: public static String register(String clazz, String name,
067: String description, String uri) throws Exception {
068: clazz = clazz.trim();
069: name = name.trim();
070: description = description.trim();
071: uri = uri.trim();
072:
073: checkValidity("<class>", clazz);
074: checkValidity("<name>", name);
075: checkValidity("<description>", description);
076: checkValidity("<CPD URI>", uri);
077:
078: boolean isNew = false;
079: chanRegStore = ChannelRegistryStoreFactory
080: .getChannelRegistryStoreImpl();
081: ChannelType[] types = chanRegStore.getChannelTypes();
082: ChannelType chanType = null;
083:
084: for (int i = 0; i < types.length && chanType == null; i++) {
085: if (types[i].getCpdUri().equals(uri))
086: chanType = types[i];
087: }
088: if (chanType == null) // new one being defined
089: {
090: isNew = true;
091: chanType = chanRegStore.newChannelType();
092: }
093:
094: chanType.setJavaClass(clazz);
095: chanType.setName(name);
096: chanType.setDescription(description);
097: chanType.setCpdUri(uri);
098: chanRegStore.saveChannelType(chanType);
099: return "The \"" + name + "\" channel type has been "
100: + (isNew ? "added" : "updated") + " successfully.";
101: }
102:
103: private static void msg(String msg) {
104: System.out.println(msg);
105: }
106:
107: private static void printHelp() {
108: msg("Usage: RegisterChannelType <class> <name> <description> <CPD URI>");
109: msg(" <class> = Fully qualified channel java class. Ex: org.jasig.portal.channels.CImage");
110: msg(" <name> = Name of channel type. Ex: Image channel");
111: msg(" <description> = Description of channel type. Ex: Displays image with optional title and subtitle");
112: msg(" <CPD URI> = URL or classpath-relative path to channel publishing document. Ex: /org/jasig/portal/channels/CImage/CImage.cpd");
113: }
114: }
|