KNOX-1105 - Provide indication that topologies were generated from simple descriptors...
[knox.git] / gateway-server / src / main / java / org / apache / hadoop / gateway / topology / xml / KnoxFormatXmlTopologyRules.java
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.apache.hadoop.gateway.topology.xml;
19
20 import org.apache.commons.digester3.Rule;
21 import org.apache.commons.digester3.binder.AbstractRulesModule;
22 import org.apache.hadoop.gateway.topology.Application;
23 import org.apache.hadoop.gateway.topology.Param;
24 import org.apache.hadoop.gateway.topology.Provider;
25 import org.apache.hadoop.gateway.topology.Service;
26 import org.apache.hadoop.gateway.topology.Version;
27 import org.apache.hadoop.gateway.topology.builder.BeanPropertyTopologyBuilder;
28 import org.xml.sax.Attributes;
29
30 public class KnoxFormatXmlTopologyRules extends AbstractRulesModule {
31
32 private static final String ROOT_TAG = "topology";
33 private static final String NAME_TAG = "name";
34 private static final String VERSION_TAG = "version";
35 private static final String DEFAULT_SERVICE_TAG = "path";
36 private static final String GENERATED_TAG = "generated";
37 private static final String APPLICATION_TAG = "application";
38 private static final String SERVICE_TAG = "service";
39 private static final String ROLE_TAG = "role";
40 private static final String URL_TAG = "url";
41 private static final String PROVIDER_TAG = "gateway/provider";
42 private static final String ENABLED_TAG = "enabled";
43 private static final String PARAM_TAG = "param";
44 private static final String VALUE_TAG = "value";
45
46 private static final Rule paramRule = new ParamRule();
47
48 @Override
49 protected void configure() {
50 forPattern( ROOT_TAG ).createObject().ofType( BeanPropertyTopologyBuilder.class );
51 forPattern( ROOT_TAG + "/" + NAME_TAG ).callMethod("name").usingElementBodyAsArgument();
52 forPattern( ROOT_TAG + "/" + VERSION_TAG ).callMethod("version").usingElementBodyAsArgument();
53 forPattern( ROOT_TAG + "/" + DEFAULT_SERVICE_TAG ).callMethod("defaultService").usingElementBodyAsArgument();
54 forPattern( ROOT_TAG + "/" + GENERATED_TAG ).callMethod("generated").usingElementBodyAsArgument();
55
56 forPattern( ROOT_TAG + "/" + APPLICATION_TAG ).createObject().ofType( Application.class ).then().setNext( "addApplication" );
57 forPattern( ROOT_TAG + "/" + APPLICATION_TAG + "/" + ROLE_TAG ).setBeanProperty();
58 forPattern( ROOT_TAG + "/" + APPLICATION_TAG + "/" + NAME_TAG ).setBeanProperty();
59 forPattern( ROOT_TAG + "/" + APPLICATION_TAG + "/" + VERSION_TAG ).createObject().ofType(Version.class).then().setBeanProperty().then().setNext("setVersion");
60 forPattern( ROOT_TAG + "/" + APPLICATION_TAG + "/" + URL_TAG ).callMethod( "addUrl" ).usingElementBodyAsArgument();
61 forPattern( ROOT_TAG + "/" + APPLICATION_TAG + "/" + PARAM_TAG ).createObject().ofType( Param.class ).then().addRule( paramRule ).then().setNext( "addParam" );
62 forPattern( ROOT_TAG + "/" + APPLICATION_TAG + "/" + PARAM_TAG + "/" + NAME_TAG ).setBeanProperty();
63 forPattern( ROOT_TAG + "/" + APPLICATION_TAG + "/" + PARAM_TAG + "/" + VALUE_TAG ).setBeanProperty();
64
65 forPattern( ROOT_TAG + "/" + SERVICE_TAG ).createObject().ofType( Service.class ).then().setNext( "addService" );
66 forPattern( ROOT_TAG + "/" + SERVICE_TAG + "/" + ROLE_TAG ).setBeanProperty();
67 forPattern( ROOT_TAG + "/" + SERVICE_TAG + "/" + NAME_TAG ).setBeanProperty();
68 forPattern( ROOT_TAG + "/" + SERVICE_TAG + "/" + VERSION_TAG ).createObject().ofType(Version.class).then().setBeanProperty().then().setNext("setVersion");
69 forPattern( ROOT_TAG + "/" + SERVICE_TAG + "/" + URL_TAG ).callMethod( "addUrl" ).usingElementBodyAsArgument();
70 forPattern( ROOT_TAG + "/" + SERVICE_TAG + "/" + PARAM_TAG ).createObject().ofType( Param.class ).then().addRule( paramRule ).then().setNext( "addParam" );
71 forPattern( ROOT_TAG + "/" + SERVICE_TAG + "/" + PARAM_TAG + "/" + NAME_TAG ).setBeanProperty();
72 forPattern( ROOT_TAG + "/" + SERVICE_TAG + "/" + PARAM_TAG + "/" + VALUE_TAG ).setBeanProperty();
73
74 forPattern( ROOT_TAG + "/" + PROVIDER_TAG ).createObject().ofType( Provider.class ).then().setNext( "addProvider" );
75 forPattern( ROOT_TAG + "/" + PROVIDER_TAG + "/" + ROLE_TAG ).setBeanProperty();
76 forPattern( ROOT_TAG + "/" + PROVIDER_TAG + "/" + ENABLED_TAG ).setBeanProperty();
77 forPattern( ROOT_TAG + "/" + PROVIDER_TAG + "/" + NAME_TAG ).setBeanProperty();
78 forPattern( ROOT_TAG + "/" + PROVIDER_TAG + "/" + PARAM_TAG ).createObject().ofType( Param.class ).then().addRule( paramRule ).then().setNext( "addParam" );
79 forPattern( ROOT_TAG + "/" + PROVIDER_TAG + "/" + PARAM_TAG + "/" + NAME_TAG ).setBeanProperty();
80 forPattern( ROOT_TAG + "/" + PROVIDER_TAG + "/" + PARAM_TAG + "/" + VALUE_TAG ).setBeanProperty();
81 }
82
83 private static class ParamRule extends Rule {
84
85 @Override
86 public void begin( String namespace, String name, Attributes attributes ) {
87 Param param = getDigester().peek();
88 String paramName = attributes.getValue( "name" );
89 if( paramName != null ) {
90 param.setName( paramName );
91 param.setValue( attributes.getValue( "value" ) );
92 }
93 }
94
95 }
96
97 }