|
Glassfish example source code file (CommonHandlers.java)
This example Glassfish source code file (CommonHandlers.java) is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.
The Glassfish CommonHandlers.java source code
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
/*
* CommonHandlers.java
*
* Created on August 30, 2006, 4:21 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package org.glassfish.admingui.common.handlers;
import com.sun.jsftemplating.annotation.Handler;
import com.sun.jsftemplating.annotation.HandlerInput;
import com.sun.jsftemplating.annotation.HandlerOutput;
import com.sun.jsftemplating.el.PageSessionResolver;
import com.sun.jsftemplating.handlers.NavigationHandlers;
import com.sun.jsftemplating.layout.descriptors.handler.HandlerContext;
import com.sun.jsftemplating.util.Util;
import java.io.IOException;
import java.io.Serializable;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.text.SimpleDateFormat;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import org.glassfish.admingui.common.util.GuiUtil;
import org.glassfish.admingui.common.util.MiscUtil;
import org.glassfish.admingui.common.util.TargetUtil;
public class CommonHandlers {
/** Creates a new instance of CommonHandlers */
public CommonHandlers() {
}
/**
* <p> This handler will be called during initialization when Cluster Support is detected.
*/
@Handler(id="initClusterSessionAttribute")
public static void initClusterSessionAttribute(HandlerContext handlerCtx){
Map sessionMap = handlerCtx.getFacesContext().getExternalContext().getSessionMap();
//The summary or detail view of deploy tables is stored in session to remember user's previous
//preference.
sessionMap.put("appSummaryView", true);
sessionMap.put("webSummaryView", true);
sessionMap.put("ejbSummaryView", true);
sessionMap.put("appclientSummaryView", true);
sessionMap.put("rarSummaryView", true);
sessionMap.put("lifecycleSummaryView", true);
sessionMap.put("adminObjectSummaryView", true);
sessionMap.put("connectorResSummaryView", true);
sessionMap.put("customResSummaryView", true);
sessionMap.put("externalResSummaryView", true);
sessionMap.put("javaMailSessionSummaryView", true);
sessionMap.put("jdbcResSummaryView", true);
sessionMap.put("jmsConnectionSummaryView", true);
sessionMap.put("jmsDestinationSummaryView", true);
}
/**
* <p> This handler will be called during initialization for doing any initialization.
*/
@Handler(id="initSessionAttributes")
public static void initSessionAttributes(HandlerContext handlerCtx){
//Ensure this method is called once per session
Object initialized = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("_SESSION_INITIALIZED");
if (initialized == null){
GuiUtil.initSessionAttributes();
}
return;
}
/**
* <p>
* This handler will be called from baseLayout.xhtml to load the maximum
* field lengths (maximum number of characters that a user can enter in each
* field).
*/
@Handler(id="getFieldLengths",
input={
@HandlerInput(name="bundle", type=java.util.ResourceBundle.class, required=true)
},
output={
@HandlerOutput(name="result", type=Map.class)
})
public static void getFieldLengths(HandlerContext handlerCtx) {
ResourceBundle bundle = (ResourceBundle) handlerCtx.getInputValue("bundle");
Map<String, Integer> result = new HashMap();
for (String key : bundle.keySet()) {
try {
result.put(key, Integer.decode(bundle.getString(key)));
} catch (NumberFormatException ex) {
// Log warning about expecting a number...
// This should never happen; if it does it's a bug, so no need to localize.
GuiUtil.getLogger().warning(
"Field length is expected to be a number, but got ('"
+ bundle.getString(key) + "') instead for key '"
+ key + "'.");
}
}
handlerCtx.setOutputValue("result", result);
}
/** This function is called in login.jsf to set the various product specific attributes such as the
* product GIFs and product names. A similar function is called for Sailfin to set Sailfin specific
* product GIFs and name.
* The function is defined in com.sun.extensions.comms.SipUtilities
*/
@Handler(id="initProductInfoAttributes")
public static void initProductInfoAttributes(HandlerContext handlerCtx) {
Map sessionMap = handlerCtx.getFacesContext().getExternalContext().getSessionMap();
//Ensure this method is called once per session
Object initialized = sessionMap.get("_INFO_SESSION_INITIALIZED");
if (initialized != null)
return;
// Initialize Product Specific Attributes
sessionMap.put("productImageURL", GuiUtil.getMessage("productImage.URL"));
sessionMap.put("productImageWidth", Integer.parseInt(GuiUtil.getMessage("productImage.width")));
sessionMap.put("productImageHeight", Integer.parseInt(GuiUtil.getMessage("productImage.height")));
sessionMap.put("loginProductImageURL", GuiUtil.getMessage("login.productImage.URL"));
sessionMap.put("loginProductImageWidth", Integer.parseInt(GuiUtil.getMessage("login.productImage.width")));
sessionMap.put("loginProductImageHeight", Integer.parseInt(GuiUtil.getMessage("login.productImage.height")));
sessionMap.put("fullProductName", GuiUtil.getMessage("versionImage.description"));
sessionMap.put("loginButtonTooltip", GuiUtil.getMessage("loginButtonTooltip"));
sessionMap.put("mastHeadDescription", GuiUtil.getMessage("mastHeadDescription"));
// showLoadBalancer is a Sailfin specific attribute. Sailfin uses Converged LB instead
// of HTTP LB. It is true for GF and false for Sailfin. In sailfin this is set in
// com.sun.extensions.comms.SipUtilities.initProductInfoAttributes() called for Sailfin in login.jsf
//TODO-V3 may need to set this back to true
//sessionMap.put("showLoadBalancer", true);
sessionMap.put("_INFO_SESSION_INITIALIZED","TRUE");
}
/**
* <p> This handler returns String[] of the given java.util.List
*
* <p> Output value: "selectedIndex" -- Type: Object /
* @param handlerCtx The HandlerContext.
*/
@Handler(id="getListElement",
input={
@HandlerInput(name="list", type=java.util.List.class, required=true ),
@HandlerInput(name="index", type=Integer.class)},
output={
@HandlerOutput(name="selectedIndex", type=Object.class)})
public static void getListElement(HandlerContext handlerCtx) {
List<String> list = (List)handlerCtx.getInputValue("list");
Integer selectedIndex = (Integer)handlerCtx.getInputValue("index");
String[] listItem = null;
if(list != null) {
if(selectedIndex == null) {
//default to 0
selectedIndex = new Integer(INDEX);
}
listItem = new String[]{list.get(selectedIndex)};
}
handlerCtx.setOutputValue("selectedIndex", listItem);
}
/**
* <p> This handler removes the given element from the list
*
* <p> Output value: "finalList" -- Type: List /
* @param handlerCtx The HandlerContext.
*/
@Handler(id="removeListElement",
input={
@HandlerInput(name="list", type=java.util.List.class, required=true ),
@HandlerInput(name="name", type=String.class)},
output={
@HandlerOutput(name="finalList", type=java.util.List.class)})
public static void removeListElement(HandlerContext handlerCtx) {
List<String> list = (List) handlerCtx.getInputValue("list");
String name = (String) handlerCtx.getInputValue("name");
String[] listItem = null;
if (list != null) {
list.remove(name);
}
handlerCtx.setOutputValue("finalList", list);
}
/**
* <p> This handler creates a map with the given keys and values
*
* <p> Output value: "map" -- Type: Map /
* @param handlerCtx The HandlerContext.
*/
@Handler(id="gf.createAttributeMap",
input={
@HandlerInput(name="keys", type=java.util.List.class),
@HandlerInput(name="values", type=java.util.List.class)},
output={
@HandlerOutput(name="map", type=java.util.Map.class)})
public static void createAttributeMap(HandlerContext handlerCtx) {
List<String> keys = (List) handlerCtx.getInputValue("keys");
List values = (List) handlerCtx.getInputValue("values");
Map<String, Object> map = new HashMap();
if (keys != null && values != null) {
for (int i = 0; i < keys.size(); i++) {
map.put(keys.get(i), values.get(i));
}
}
handlerCtx.setOutputValue("map", map);
}
/**
* <p> This handler returns the encoded String using the type specified.
* <p> If type is not specified, it defaults to UTF-8.
* <p> Input value: "value" -- Type: String
* <p> Input value: "delim" -- Type: String
* <p> Input Value: "type" -- Type: String
* <p> Output Value: "value" -- Type: String
*@param handlerCtx The HandlerContext.
*/
@Handler(id="selectiveEncode",
input={
@HandlerInput(name="value", type=String.class, required=true ),
@HandlerInput(name="delim", type=String.class),
@HandlerInput(name="type", type=String.class)},
output={
@HandlerOutput(name="result", type=String.class)}
)
public static void selectiveEncode(HandlerContext handlerCtx) {
String value = (String) handlerCtx.getInputValue("value");
String delim = (String) handlerCtx.getInputValue("delim");
String encType = (String) handlerCtx.getInputValue("type");
String encodedString = GuiUtil.encode(value, delim, encType);
handlerCtx.setOutputValue("result", encodedString);
}
/**
* <p> This method kills the session, and logs out
* Server Domain Attributes Page.</p>
* <p> Input value: "page" -- Type: java.lang.String
* @param handlerCtx The HandlerContext.
*/
@Handler(id="logout")
public static void logout(HandlerContext handlerCtx) {
handlerCtx.getFacesContext().getExternalContext().invalidateSession();
}
/**
* <p> This method sets the required attribute of a UI component .
* <p> Input value: "id" -- Type: java.lang.String
* <p> Input value: "required" -- Type: java.lang.String
* @param handlerCtx The HandlerContext.
*/
@Handler(id="setComponentRequired",
input={
@HandlerInput(name="id", type=String.class, required=true),
@HandlerInput(name="required", type=String.class, required=true)
})
public static void setComponentRequired(HandlerContext handlerCtx) {
String id = (String) handlerCtx.getInputValue("id");
String required = (String) handlerCtx.getInputValue("required");
UIComponent viewRoot = handlerCtx.getFacesContext().getViewRoot();
if (viewRoot == null) return;
try {
UIInput targetComponent = (UIInput) viewRoot.findComponent(id);
if (targetComponent != null ){
targetComponent.setRequired(Boolean.valueOf(required));
}
}catch(Exception ex){
//Cannot find component, do nothing.
}
}
/**
* <p> Test if a particular attribute exists.
* It will look at request scope, then page, then session.
*/
@Handler(id="testExists",
input={
@HandlerInput(name="attr", type=String.class, required=true )},
output={
@HandlerOutput(name="defined", type=Boolean.class)}
)
public static void testExists(HandlerContext handlerCtx) {
String attr = (String) handlerCtx.getInputValue("attr");
if(GuiUtil.isEmpty(attr)){
handlerCtx.setOutputValue("defined", false);
}else{
handlerCtx.setOutputValue("defined", true);
}
}
/**
* <p> Remove the properties if the key(name) is empty.
*
*/
@Handler(id="removeEmptyProps",
input={
@HandlerInput(name="props", type=List.class, required=true )},
output={
@HandlerOutput(name="modifiedProps", type=List.class)}
)
public static void removeEmptyProps(HandlerContext handlerCtx) {
List<Map props = (List
Other Glassfish examples (source code examples)
Here is a short list of links related to this Glassfish CommonHandlers.java source code file:
|