com.sun.jsftemplating.layout.descriptors.Resource Maven / Gradle / Ivy
/*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the license at
* https://jsftemplating.dev.java.net/cddl1.html or
* jsftemplating/cddl1.txt.
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* at jsftemplating/cddl1.txt.
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* you own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
*/
package com.sun.jsftemplating.layout.descriptors;
import com.sun.jsftemplating.resource.ResourceFactory;
import com.sun.jsftemplating.util.Util;
/**
* This class holds information that describes a Resource. It
* provides access to a {@link ResourceFactory} for obtaining the
* actual Resource object described by this descriptor. See the
* layout.dtd file for more information on how to define a Resource
* via XML. The LayoutDefinition will add all defined Resources to
* the request scope for easy access (including via JSF EL).
*
* @author Ken Paulsen ([email protected])
*/
public class Resource implements java.io.Serializable {
private static final long serialVersionUID = 1L;
/**
* This is the id for the Resource.
*/
private String _id = null;
/**
* This holds "extraInfo" for the Resource, such as a ResourceBundle
* baseName.
*/
private String _extraInfo = null;
/**
* This is a String className for the Factory.
*/
private String _factoryClass = null;
/**
* The Factory that produces the desired UIComponent
.
*/
private transient ResourceFactory _factory = null;
/**
* Constructor.
*/
public Resource(String id, String extraInfo, String factoryClass) {
if (id == null) {
throw new NullPointerException("'id' cannot be null!");
}
if (factoryClass == null) {
throw new NullPointerException("'factoryClass' cannot be null!");
}
_id = id;
_extraInfo = extraInfo;
_factoryClass = factoryClass;
_factory = createFactory();
}
/**
* Accessor method for ID. This is the key the resource will be stored
* under in the Request scope.
*/
public String getId() {
return _id;
}
/**
* This holds "extraInfo" for the Resource, such as a
* ResourceBundle
baseName.
*/
public String getExtraInfo() {
return _extraInfo;
}
/**
* This method provides access to the {@link ResourceFactory}.
*
* @return The {@link ResourceFactory}.
*/
public ResourceFactory getFactory() {
if (_factory == null) {
_factory = createFactory();
}
return _factory;
}
/**
* This method creates a new {@link ResourceFactory}.
*
* @return The new {@link ResourceFactory}.
*/
protected ResourceFactory createFactory() {
try {
Class cls = Util.loadClass(_factoryClass, _factoryClass);
return (ResourceFactory) cls.newInstance();
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
} catch (InstantiationException ex) {
throw new RuntimeException(ex);
} catch (IllegalAccessException ex) {
throw new RuntimeException(ex);
}
}
}