📄 webcontextmaptemplate.java
字号:
package com.esri.solutions.jitk.common.templates.map;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.media.jai.JAI;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import com.esri.adf.web.ags.data.AGSLocalMapResource;
import com.esri.adf.web.ags.data.AGSMapResource;
import com.esri.adf.web.aims.data.AIMSMapResource;
import com.esri.adf.web.data.GISResource;
import com.esri.adf.web.data.WebContext;
import com.esri.adf.web.data.WebContextInitialize;
import com.esri.adf.web.data.WebMap;
import com.esri.adf.web.data.geometry.WebExtent;
import com.esri.adf.web.wms.data.WMSMapResource;
import com.esri.solutions.jitk.common.export.image.WebMapImageExporter;
import com.esri.solutions.jitk.web.data.IGISResourceFactory;
import com.esri.solutions.jitk.web.wcs.data.WCSMapResource;
import com.esri.solutions.jitk.web.wfs.data.WFSMapResource;
import com.sun.media.jai.codec.SeekableStream;
/**
* Implementation of a Map Template that uses the resources
* within a {@link WebContext} to define the Map Template.
*/
public class WebContextMapTemplate extends BaseMapTemplate implements IMapTemplate, WebContextInitialize {
private static final String INFO_CACHING_PREVIEW = "Caching Preview for Map Template [{0}, {1}]";
private static final String INFO_GENERATING_PREVIEW = "Generating Preview for Map Template [{0}, {1}]";
private static final String INFO_USING_CACHED_PREVIEW = "Using Cached Preview for Map Template [{0}, {1}]";
private static final String WARN_UNABLE_TO_GEN_PREVIEW = "Unable to generate Preview for Map Template [{0},{1}]";
private static final String ERROR_NULL_WEB_MAP = "WebMap object is required to be within WebContext, but is null.";
private static final String ERROR_NULL_WEB_CTX = "WebContext object is required, and cannot be null.";
private static final Logger LOG = LogManager.getLogger(WebContextMapTemplate.class);
/**
* Reference to WebContext that contains the resources
* for the Map Template.
*/
private WebContext m_templateCtx;
/**
* Reference to Web Map Image Exporter to export the web map to a byte[].
*/
private WebMapImageExporter m_mapExporter;
/**
* Indicates if the Preview Image should be cached internally. Optional, <code>false</code>
* by default.
*/
private boolean m_cachePreview = true;
/**
* Cached Preview image if Caching is turned on.
*/
private RenderedImage m_preview;
/**
* Indicates if the template has been initialized within
* the current Web Context.
*/
private boolean m_initialized = false;
/**
* The Web Context that this template is currently loaded into.
*/
private WebContext m_currentCtx;
/**
* Contains references to a set of GIS Resource Factories so that
* new GIS Resources can be created from the GIS Resources already
* within the internal WebContext.
*/
private Map<String, IGISResourceFactory<GISResource>> m_factories;
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.common.templates.map.IMapTemplate#getPreview()
*/
public RenderedImage getPreview() {
if (m_templateCtx == null) {
LOG.error(ERROR_NULL_WEB_CTX);
throw new IllegalStateException (ERROR_NULL_WEB_CTX);
}
if (m_templateCtx.getWebMap() == null) {
LOG.error(ERROR_NULL_WEB_MAP);
throw new IllegalStateException (ERROR_NULL_WEB_MAP);
}
RenderedImage preview = null;
try {
if (m_cachePreview && m_preview != null) {
if (LOG.isInfoEnabled()) {
LOG.info(MessageFormat.format(INFO_USING_CACHED_PREVIEW, m_id, m_name));
}
return m_preview;
}
if (LOG.isInfoEnabled()) {
LOG.info(MessageFormat.format(INFO_GENERATING_PREVIEW, m_id, m_name));
}
m_mapExporter.setWebMap(m_templateCtx.getWebMap());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
m_mapExporter.exportImage(baos);
ParameterBlock pb = new ParameterBlock();
pb.add(SeekableStream.wrapInputStream(new ByteArrayInputStream(baos.toByteArray()), true));
preview = JAI.create("stream", pb);
if (m_cachePreview) {
if (LOG.isInfoEnabled()) {
LOG.info(MessageFormat.format(INFO_CACHING_PREVIEW, m_id, m_name));
}
m_preview = preview;
}
} catch (IOException e) {
LOG.warn(MessageFormat.format(WARN_UNABLE_TO_GEN_PREVIEW, m_id, m_name), e);
}
return preview;
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.common.templates.map.IWebContextOpenable#open(com.esri.adf.web.data.WebContext)
*/
public void open(WebContext ctx) {
if (ctx == null) {
throw new NullPointerException ();
}
WebExtent extent = m_templateCtx.getWebMap().getFullExtent();
Map<String, GISResource> resources = new LinkedHashMap<String,GISResource>(m_templateCtx.getResources());
Map<String, GISResource> newResources = new LinkedHashMap<String, GISResource>();
for (Entry<String, GISResource> entry : resources.entrySet()) {
newResources.put(entry.getKey(), copy(entry.getValue()));
}
ctx.setResources(newResources);
ctx.setSpatialReference(m_templateCtx.getSpatialReference());
ctx.getWebMap().setCurrentExtent(extent);
ctx.setInit(true);
}
/**
* Sets the {@link WebContext} that contains the {@link GISResource}s
* to use for this Map Template. The Web Context object is required
* and cannot be <code>null</code>. The Web Context must have a
* {@link WebMap} associated with it in order to generate the preview
* of the Map Template.
*
* @param ctx Reference to Web Context to use for this Map Template,
* cannot be <code>null</code>.
* @throws NullPointerException Thrown if the <code>ctx</code>
* argument is <code>null</code>.
*/
public void setWebContext (WebContext ctx) {
if (ctx == null) {
throw new NullPointerException ();
}
m_templateCtx = ctx;
if (!m_templateCtx.isInit()) {
m_templateCtx.setInit(true);
}
}
/**
* Returns the {@link WebContext} object that is used for this
* Map Template. May return <code>null</code> if not preceded
* by an invocation of {@link #setWebContext(WebContext)}.
*
* @return Associated Web Context object.
*/
public WebContext getWebContext () {
return m_templateCtx;
}
/**
* Sets the reference to the {@link WebMapImageExporter} to use
* to export the map image from a {@link WebMap}. The map image
* will be used as the Preview of this Map Template.
*
* @param exporter Reference to {@link WebMapImageExporter}, cannot
* be <code>null</code>.
* @throws NullPointerException Thrown if the <code>exporter</code>
* argument is <code>null</code>.
*/
public void setWebMapImageExporter (WebMapImageExporter exporter) {
if (exporter == null) {
throw new NullPointerException();
}
m_mapExporter = exporter;
}
/**
* Returns the flag indicating if the Preview Image should be cached.
* Caching of the Preview Image is enabled by default.
*
* @return Flag indicating if the Preview Image should be cached.
*/
public boolean getCachePreview () {
return m_cachePreview;
}
/**
* Sets the flag to indicate whether the Preview image should be
* cached internally. If memory space permits, enabling the cache
* should speed up performance. After setting this flag to <code>true</code>
* the next time the {@link #getPreview()} method is invoked the Preview
* Image will be cached.
*
* @param flag Indicates whether the Preview image should be cached.
*/
public void setCachePreview (boolean flag) {
m_cachePreview = flag;
if (!m_cachePreview) {
m_preview = null;
}
}
/*
* (non-Javadoc)
* @see com.esri.adf.web.data.WebContextInitialize#destroy()
*/
public void destroy() {
m_currentCtx = null;
m_initialized = false;
}
/*
* (non-Javadoc)
* @see com.esri.adf.web.data.WebContextInitialize#init(com.esri.adf.web.data.WebContext)
*/
public void init(WebContext ctx) {
m_currentCtx = ctx;
if (!m_initialized) {
m_initialized = true;
getPreview();
open(m_currentCtx);
}
}
/**
* Sets the collection of GIS Resource Factories for creating copies
* of GIS Resources already in the internal WebContext. The collection
* is a Map indexed by the GIS Resource Class Name.
*
* @param factories Collection of GIS Resource Factories.
*/
public void setGisResourceFactories (Map<String, IGISResourceFactory<GISResource>> factories) {
m_factories = factories;
}
/**
* Creates a copy of an existing GIS Resource. This method returns
* a copy of a GIS Resource that does not exist within any WebContext.
*
* @param resource Reference to a GIS Resource.
* @return Copy of GIS Resource.
*/
protected GISResource copy (GISResource resource) {
GISResource copy = null;
if (resource instanceof AGSLocalMapResource) {
copy = copy((AGSLocalMapResource)resource);
} else if (resource instanceof AGSMapResource) {
copy = copy ((AGSMapResource)resource);
} else if (resource instanceof AIMSMapResource) {
copy = copy ((AIMSMapResource)resource);
} else if (resource instanceof WMSMapResource) {
copy = copy((WMSMapResource)resource);
} else if (resource instanceof WCSMapResource) {
copy = copy((WCSMapResource)resource);
} else if (resource instanceof WFSMapResource) {
copy = copy((WFSMapResource)resource);
} else {
copy = resource;
}
return copy;
}
/**
* Creates a copy of an {@link AGSMapResource}. This method
* will use the appropriate GIS Resource Factory to create
* a new object and then will initialize relevant property values
* on the copied GIS Resource.
*
* @param resource GIS Resource to copy
* @return Copy of specified GIS Resource.
*/
protected AGSMapResource copy (AGSMapResource resource) {
AGSMapResource copy = null;
IGISResourceFactory<GISResource> factory =
m_factories.get(AGSMapResource.class.getName());
if (factory == null) {
return null;
}
copy = (AGSMapResource) factory.createResource();
copy.setAlias(resource.getAlias());
copy.setEndPointURL(resource.getEndPointURL());
copy.setUser(resource.getUser());
return copy;
}
/**
* Creates a copy of an {@link AGSLocalMapResource}. This method
* will use the appropriate GIS Resource Factory to create
* a new object and then will initialize relevant property values
* on the copied GIS Resource.
*
* @param resource GIS Resource to copy
* @return Copy of specified GIS Resource.
*/
protected AGSLocalMapResource copy (AGSLocalMapResource resource) {
AGSLocalMapResource copy = null;
IGISResourceFactory<GISResource> factory =
m_factories.get(AGSLocalMapResource.class.getName());
if (factory == null) {
return null;
}
copy = (AGSLocalMapResource) factory.createResource();
copy.setAlias(resource.getAlias());
copy.setHosts(resource.getHosts());
copy.setServerObjectName(resource.getServerObjectName());
copy.setClusterType(resource.getClusterType());
copy.setFailRecheckValue(resource.getFailRecheckValue());
copy.setUser(resource.getUser());
return copy;
}
/**
* Creates a copy of an {@link AIMSMapResource}. This method
* will use the appropriate GIS Resource Factory to create
* a new object and then will initialize relevant property values
* on the copied GIS Resource.
*
* @param resource GIS Resource to copy
* @return Copy of specified GIS Resource.
*/
protected AIMSMapResource copy (AIMSMapResource resource) {
AIMSMapResource copy = null;
IGISResourceFactory<GISResource> factory =
m_factories.get(AIMSMapResource.class.getName());
if (factory == null) {
return null;
}
copy = (AIMSMapResource) factory.createResource();
copy.setAlias(resource.getAlias());
copy.setHostName(resource.getHostName());
copy.setPassword(resource.getPassword());
copy.setPasswordEncrypted(resource.isPasswordEncrypted());
copy.setPort(resource.getPort());
copy.setServiceName(resource.getServiceName());
copy.setUserName(resource.getUserName());
return copy;
}
/**
* Creates a copy of an {@link WMSMapResource}. This method
* will use the appropriate GIS Resource Factory to create
* a new object and then will initialize relevant property values
* on the copied GIS Resource.
*
* @param resource GIS Resource to copy
* @return Copy of specified GIS Resource.
*/
protected WMSMapResource copy (WMSMapResource resource) {
WMSMapResource copy = null;
IGISResourceFactory<GISResource> factory =
m_factories.get(WMSMapResource.class.getName());
if (factory == null) {
return null;
}
copy = (WMSMapResource) factory.createResource();
copy.setAlias(resource.getAlias());
copy.setWmsURL(resource.getWmsURL());
copy.setWmsVersion(resource.getWmsVersion());
return copy;
}
/**
* Creates a copy of an {@link WCSMapResource}. This method
* will use the appropriate GIS Resource Factory to create
* a new object and then will initialize relevant property values
* on the copied GIS Resource.
*
* @param resource GIS Resource to copy
* @return Copy of specified GIS Resource.
*/
protected WCSMapResource copy (WCSMapResource resource) {
WCSMapResource copy = null;
IGISResourceFactory<GISResource> factory =
m_factories.get(WCSMapResource.class.getName());
if (factory == null) {
return null;
}
copy = (WCSMapResource)factory.createResource();
copy.setAlias(resource.getAlias());
copy.setCoverage(resource.getCoverage());
copy.setEndPointURL(resource.getEndPointURL());
return copy;
}
/**
* Creates a copy of an {@link WFSMapResource}. This method
* will use the appropriate GIS Resource Factory to create
* a new object and then will initialize relevant property values
* on the copied GIS Resource.
*
* @param resource GIS Resource to copy
* @return Copy of specified GIS Resource.
*/
protected WFSMapResource copy (WFSMapResource resource) {
WFSMapResource copy = null;
IGISResourceFactory<GISResource> factory =
m_factories.get(WFSMapResource.class.getName());
if (factory == null) {
return null;
}
copy = (WFSMapResource) factory.createResource();
copy.setAlias(resource.getAlias());
copy.setEndPointURL(resource.getEndPointURL());
return copy;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -