/*
* Copyright (C) 2011-2014 GeoForge Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.geoforge.worldwindogc.factory;
import gov.nasa.worldwind.Configuration;
import gov.nasa.worldwind.Factory;
import gov.nasa.worldwind.WorldWind;
import gov.nasa.worldwind.avlist.AVKey;
import gov.nasa.worldwind.avlist.AVList;
import gov.nasa.worldwind.avlist.AVListImpl;
import gov.nasa.worldwind.layers.Layer;
import gov.nasa.worldwind.ogc.wms.WMSLayerCapabilities;
import java.util.List;
import java.util.logging.Logger;
import org.geoforge.worldwindogc.layer.GfrLyrWMSTiledImageLayer;
import org.geoforge.java.util.logging.filehandler.FileHandlerLogger;
import org.geoforge.worldwindogc.capabilities.GfrOGCCapabilities;
import org.geoforge.worldwindogc.capabilities.GfrWMSCapabilities;
/**
*
* @author bantchao
*/
public class GfrBasicLayerFactory extends GfrBasicFactory
{
final static public String GFR_KEY_LAYER = "org.geoforge.worldwind.avkey.LayerFactory";
static
{
if (Configuration.getStringValue(GfrBasicLayerFactory.GFR_KEY_LAYER) == null)
Configuration.setValue(GfrBasicLayerFactory.GFR_KEY_LAYER,
GfrBasicLayerFactory.class.getName());
}
// ----
// begin: instantiate logger for this class
final private static Logger _LOGGER_ = Logger.getLogger(GfrBasicLayerFactory.class.getName());
static
{
GfrBasicLayerFactory._LOGGER_.addHandler(FileHandlerLogger.s_getInstance());
}
// end: instantiate logger for this class
// ----
public GfrBasicLayerFactory()
{
super();
}
static public Object s_createComponent(GfrWMSCapabilities caps, AVList params)
{
AVList configParams = params.copy(); // Copy to insulate changes from the caller.
// Some wms servers are slow, so increase the timeouts and limits used by world wind's retrievers.
configParams.setValue(AVKey.URL_CONNECT_TIMEOUT, 30000);
configParams.setValue(AVKey.URL_READ_TIMEOUT, 30000);
configParams.setValue(AVKey.RETRIEVAL_QUEUE_STALE_REQUEST_LIMIT, 60000);
try
{
Factory factory = (Factory) WorldWind.createConfigurationComponent(GfrBasicLayerFactory.GFR_KEY_LAYER);
Object obj = factory.createFromConfigSource(caps, configParams);
return obj;
}
catch(IllegalStateException excIllegalState)
{
String str = excIllegalState.getMessage() + "\ngot excIllegalState, ignoring ...";
GfrBasicLayerFactory._LOGGER_.warning(str);
return null;
}
catch(IllegalArgumentException excIllegalArgument)
{
String str = excIllegalArgument.getMessage() + "\ngot excIllegalArgument, ignoring ...";
GfrBasicLayerFactory._LOGGER_.warning(str);
return null;
}
}
@Override
protected Layer doCreateFromCapabilities(GfrOGCCapabilities caps, AVList params)
{
String serviceName = caps.getServiceInformation().getServiceName();
if (serviceName == null)
{
String str = "serviceName == null";
GfrBasicLayerFactory._LOGGER_.severe(str);
throw new IllegalArgumentException(str);
}
/*/
if (! serviceName.equalsIgnoreCase(OGCConstants.WMS_SERVICE_NAME))
{
String str = "! serviceName.equalsIgnoreCase(OGCConstants.WMS_SERVICE_NAME), serviceName=" + serviceName;
GfrBasicLayerFactory._LOGGER_.severe(str);
throw new IllegalArgumentException(str);
}
if (serviceName.equalsIgnoreCase("WMS"))
{
String str = "serviceName.equalsIgnoreCase(\"WMS\")";
GfrBasicLayerFactory._LOGGER_.severe(str);
throw new IllegalArgumentException(str);
}
//*/
if (params == null)
params = new AVListImpl();
if (params.getStringValue(AVKey.LAYER_NAMES) == null)
{
// Use the first named layer since no other guidance given
List<WMSLayerCapabilities> namedLayers = ((GfrWMSCapabilities) caps).getNamedLayers();
if (namedLayers == null)
{
String str = "namedLayers == null, serviceName=" + serviceName;
GfrBasicLayerFactory._LOGGER_.severe(str);
throw new IllegalStateException(str);
}
if (namedLayers.isEmpty())
{
String str = "namedLayers.isEmpty(), serviceName=" + serviceName;
GfrBasicLayerFactory._LOGGER_.severe(str);
throw new IllegalStateException(str);
}
if (namedLayers.get(0) == null)
{
String str = "namedLayers.get(0) == null, serviceName=" + serviceName;
GfrBasicLayerFactory._LOGGER_.severe(str);
throw new IllegalStateException(str);
}
params.setValue(AVKey.LAYER_NAMES, namedLayers.get(0).getName());
}
try
{
GfrLyrWMSTiledImageLayer lyr = new GfrLyrWMSTiledImageLayer((GfrWMSCapabilities) caps, params);
return lyr;
}
catch(Exception exc)
{
//exc.printStackTrace();
System.err.println("exc caught, returning nil");
return null;
}
}
}