Extending Service Component |
Below demonstrates the implementation of the component layer:
@Component(providerTypes={MapProvider.class},optional=false, type = "PushpinMap") public class PushpinComponent implements ComponentContextAware{ ... }
@Component annotates the meta information of the service component, with type indicating the type the service component, providerTypes indicating the service providers associated with the service component, and nameMapping indicating an implementation class of the NameMapping class, which is used to provide service interface information when clustering and is required when clustering is employed (SuperMap iServer supports for clustering extended services, please refer to Building DSS Cluster).
In the implementation class of the service interface, ComponentContextAware can be used to get the service component context, through which the configuration information of the of the service providers and service components is provided.
Note: To improve the flexibility and make the structure of the domain service clearer, it is suggested that the business logic should not be placed on the business component layer. It is suggested that the business logic should be placed on the service provider layer, with the business capabilities implemented through the service providers by the service components.
Getting service component configuration
The configuration information of the service components is in iserver-services.xml (please refer to iServer configuration file for more details). Below is the configuration for a service component:
<component class="com.supermap.sample.SampleComponent" interfacenames="rest" name="samplecomponent" providers="ugcMapProvider-China400"> <config class="com.supermap.sample.SampleComponentConfig"> <param1>default</param1> ... </config> </component>
SampleComponentConfig is the configuration class for SampleComponent, with param1 corresponding to the property of the SampleComponentConfig class. "rest" is the name of the interface to publish the Web service, "ugcMapProvider-China400" is the service provider used by the service component (Please refer to Service configuration file Structure.
In the implementation class of the service component, ComponentContextAware can be used to get the service component context, through which the configuration information of the service component can be got. The code is as follows:
@Component(providerTypes={MapProvider.class},optional=false, type = "SampleType") public class SampleComponent implements ComponentContextAware{ ... public void setComponentContext(ComponentContext context) { SampleComponentConfig config=context.getConfig(SampleComponentConfig.class); ... } }
The SampleComponentConfig object corresponds to the <config class="com.supermap.sample.SampleComponentConfig"/> configuration item.
Getting service components
In SuperMap iServer, the service component can get service providers, therefore getting the corresponding GIS capabilities. The code is as follows:
@Component(providerTypes={MapProvider.class},optional=false, type = "SampleType") public class SampleComponent implements ComponentContextAware{ ... public void setComponentContext(ComponentContext context) { List<MapProvider> mapproviders=context.getProviders(MapProvider.class); ... } }
By extending and adding new service components, exsisting service providers can be resued to implement richer capabilities.
In this example, a new service component PushpinComponent is implemented using the MapProvider to provide the labeling capability on the map.
Below is the implementation code of PushpinComponent:
package com.supermap.sample.component; import java.util.List; import com.supermap.services.components.Component; import com.supermap.services.components.ComponentContext; import com.supermap.services.components.ComponentContextAware; import com.supermap.services.components.commontypes.Geometry; import com.supermap.services.components.commontypes.HighlightTarget; import com.supermap.services.components.commontypes.ImageOutputOption; import com.supermap.services.components.commontypes.MapImage; import com.supermap.services.components.commontypes.MapParameter; import com.supermap.services.components.commontypes.OutputFormat; import com.supermap.services.components.commontypes.Point2D; import com.supermap.services.components.commontypes.Rectangle; import com.supermap.services.components.commontypes.Rectangle2D; import com.supermap.services.components.commontypes.RectifyType; import com.supermap.services.components.commontypes.ReturnType; import com.supermap.services.components.commontypes.Style; import com.supermap.services.components.commontypes.TrackingLayer; import com.supermap.services.components.spi.MapProvider; @Component(providerTypes={MapProvider.class},optional=false, type = "PushpinMap") public class PushpinComponent implements ComponentContextAware{ private MapProvider mapprovider =null; private MapParameter defaultMapParam=null; //Output with default parameters and label the position represented with pushpinX and pushpinY. public String GetImage(double pushpinX,double pushpinY){ //Draw pushpinX and pushpinY on the tracking layer TrackingLayer trackinglayer=new TrackingLayer(); HighlightTarget highlightTarget=new HighlightTarget(); Geometry pushpinGeometry=Geometry.fromPoint2D(new Point2D(pushpinX, pushpinY)); // Use the flag symbol in China.smwu Style style=new Style(); style.markerSize=8; style.markerSymbolID = 252178; highlightTarget.style=style; highlightTarget.geometries=new Geometry[]{pushpinGeometry}; trackinglayer.highlightTargets.add(highlightTarget); if(mapprovider!=null&defaultMapParam!=null){ defaultMapParam.trackingLayer=trackinglayer; defaultMapParam.returnType = ReturnType.DEFAULT; //The picture size defaultMapParam.viewer = new Rectangle(0, 0, 500, 250); // The geographic bounds for map output defaultMapParam.viewBounds = new Rectangle2D(12000000, 4000000, 14000000, 6000000); //Output map with the geographic bounds defaultMapParam.rectifyType=RectifyType.BYVIEWBOUNDS; ImageOutputOption outputOption = new ImageOutputOption(); // The specified output format outputOption.format = OutputFormat.PNG; MapImage mapImage = mapprovider.getMapImage(defaultMapParam, outputOption); String imageURI=mapImage.imageUrl; imageURI=imageURI.replace("{ip}", "localhost"); imageURI=imageURI.replace("{port}", "8090"); return imageURI; } return "Output failure"; } public void setComponentContext(ComponentContext context) { PushpinCofig config=context.getConfig(PushpinCofig.class); String mapName=config.getMapName(); List<MapProvider> mapproviders=context.getProviders(MapProvider.class); if(!mapproviders.isEmpty()){ this.mapprovider=mapproviders.get(0); this.defaultMapParam=mapprovider.getDefaultMapParameter(mapName); } } }
public String GetImage(double pushpinX,double pushpinY) returns the URI of a map image. The position of (pushpinX,pushpinY) on the map is labeled with a red flag.
Note: The URI of the image contains the server IP and port number. In this example, the IP and port need to be processed in the service component. Here we replace the IP and port with localhost and 8090, as shown below:
imageURI=imageURI.replace("{ip}", "localhost"); imageURI=imageURI.replace("{port}", "8090");
Below is the implementation of the configuration class for the service component:
package com.supermap.sample.component; public class PushpinCofig { private String mapName; public String getMapName() { return mapName; } public void setMapName(String mapName) { this.mapName = mapName; } }
Place the Jar package after compiling in Java_HOME%\webapps\iserver\WEB-INF\lib.
As an example, PushpinComponent uses "China" in the China.smwu workspace for output, with ugcProvider-China400 being the service provider. The configuration in iserver-services.xml is as follows:
<component class="com.supermap.sample.component.PushpinComponent" interfaceNames="rest" name="map-pushpin" providers="ugcMapProvider-China400"> <config class="com.supermap.sample.component.PushpinCofig"> <mapName>China</mapName> </config> </component>
interfaceNames="rest" indicates that the service component will be published as REST service. SuperMap iServer can publish any service component as REST service through the Restlet mechanism (refer to Publishing Domain Service Component as REST Service).
In the http://localhost:8090/iserver/services/map-pushpin/rest/domainComponents/PushpinComponent page, we can see resource list mapped by the PushpinComponent method. Click the GetImageResult resource and type in html format parameters, such as 13000000.0,4800000.0, to get xml format representation, or access the URI below:
http://localhost:8090/iserver/services/map-pushpin/rest/domainComponents/PushpinComponent/GetImageResult.xml?arg0=13000000&arg1=4800000
The response representation in XML format is as follows:
<string> http://localhost:8090//iserver/output/temp/China_1f4xfa/30236220/10x7/4129x3129_89252943.png?_t=1540880753569 </string>
We can get the map access URI, by accessing which the following map image will be got: