Java tutorial
/* * Copyright (C) 2012 Dario Scoppelletti, <http://www.scoppelletti.it/>. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package it.scoppelletti.programmerpower.web.spring.config; import javax.servlet.*; import org.springframework.core.env.*; import org.springframework.web.context.*; /** * Sorgente per la sostituzione dei riferimenti alle proprietà nelle * risorse di definizione dei bean. * * <P>Il componente {@code WebPropertySource} implementa alcune proprietà * specifiche di un’applicazione Web Servlet.</P> * * <P><TABLE WIDTH="100%" BORDER="1" CELLPADDING="5"> * <THEAD> * <TR> * <TH>Proprietà</TH> * <TH>Descrizione</TH> * </TR> * </THEAD> * <TBODY> * <TR> * <TD>{@code it.scoppelletti.param.contextPath}</TD> * <TD>Valore della proprietà {@code contextPath} dell’oggetto * {@code ServletContext}.</TD> * </TR> * </TBODY> * </TABLE></P> * * @see <A HREF="{@docRoot}/../reference/setup/envprops.html" * TARGET="_top">Proprietà di ambiente</A> * @since 1.0.0 */ public final class WebPropertySource extends PropertySource<WebApplicationContext> { /** * Nome della proprietà corrispondente alla proprietà * {@code contextPath} dell’oggetto {@code ServletContext}. Il valore * della costante è <CODE>{@value}</CODE>. */ public static final String PROP_CONTEXTPATH = "it.scoppelletti.param.contextPath"; /** * Costruttore. * * @param name Nome del componente. * @param applCtx Contesto dell’applicazione. */ public WebPropertySource(String name, WebApplicationContext applCtx) { super(name, applCtx); } /** * Restituisce il valore di una proprietà * * @param name Nome della proprietà. * @return Valore. Se la proprietà non è impostata, * restituisce {@code null}. */ @Override public Object getProperty(String name) { ServletContext servletCtx; if (WebPropertySource.PROP_CONTEXTPATH.equals(name)) { servletCtx = getSource().getServletContext(); if (servletCtx == null) { logger.warn("Servlet content not set."); return null; } return getContextPath(servletCtx); } return null; } /** * Restituisce il percorso del contesto dell’applicazione Web. * * @param servletCtx Contesto dell’applicazione Web. * @return Valore. */ private String getContextPath(ServletContext servletCtx) { String value = servletCtx.getContextPath(); if (value == null) { return null; } if (value.startsWith("/")) { return value.substring(1); } return value; } }