View Javadoc

1   /* 
2    * Copyright (c) 2007, Fraunhofer-Gesellschaft
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    * 
9    * (1) Redistributions of source code must retain the above copyright
10   *     notice, this list of conditions and the disclaimer at the end.
11   *     Redistributions in binary form must reproduce the above copyright
12   *     notice, this list of conditions and the following disclaimer in
13   *     the documentation and/or other materials provided with the
14   *     distribution.
15   * 
16   * (2) Neither the name of Fraunhofer nor the names of its
17   *     contributors may be used to endorse or promote products derived
18   *     from this software without specific prior written permission.
19   * 
20   * DISCLAIMER
21   * 
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   *  
34   */
35  package org.ogf.graap.wsag.wsrf.sg.impl;
36  
37  import javax.xml.namespace.QName;
38  
39  import org.apache.log4j.Logger;
40  import org.apache.muse.ws.addressing.EndpointReference;
41  import org.apache.muse.ws.metadata.remote.MetadataExchangeClient;
42  import org.apache.muse.ws.resource.sg.impl.SimpleMembershipContentRule;
43  import org.apache.xmlbeans.XmlException;
44  import org.apache.xmlbeans.XmlObject;
45  import org.ogf.graap.wsag.api.WsagConstants;
46  import org.ogf.graap.wsag4j.types.engine.WSAG4JMetadataDocument;
47  import org.w3c.dom.Element;
48  
49  /**
50   * MembershipContentRule
51   * 
52   * @author Oliver Waeldrich
53   * 
54   */
55  public class MembershipContentRule extends SimpleMembershipContentRule
56  {
57  
58      private static final Logger LOG = Logger.getLogger( MembershipContentRule.class );
59  
60      /**
61       * default constructor
62       */
63      public MembershipContentRule()
64      {
65          super();
66      }
67  
68      /**
69       * Creates a {@link MembershipContentRule} based on the given XML representaotion of the rule.
70       * 
71       * @param xml
72       *            the rule XML representation
73       * 
74       * @see SimpleMembershipContentRule
75       */
76      public MembershipContentRule( Element xml )
77      {
78          super( xml );
79      }
80  
81      /**
82       * Matches the resource referenced by the given EPR against this rule.
83       * 
84       * @param memberEPR
85       *            the EPR of the resource to match
86       * 
87       * @return <code>true</code> if the resource matches this rule, <code>false</code> otherwise
88       */
89      public boolean isMatch( EndpointReference memberEPR )
90      {
91          boolean result = super.isMatch( memberEPR );
92  
93          if ( result )
94          {
95              QName memberInterface = getMemberInterface();
96  
97              if ( memberInterface != null )
98              {
99                  //
100                 // For Resources that are created locally the port type QName is set
101                 // in the MembershipContentRuleContext. If not, the resource was not
102                 // created locally and we must use MEX to lookup the port type.
103                 //
104 
105                 QName porttype = MembershipContentRuleContext.getPortTypeQName();
106 
107                 if ( porttype != null )
108                 {
109                     boolean isMember =
110                         memberInterface.getNamespaceURI().equals( porttype.getNamespaceURI() )
111                             && memberInterface.getLocalPart().equals( porttype.getLocalPart() );
112 
113                     if ( LOG.isDebugEnabled() )
114                     {
115                         if ( isMember )
116                         {
117                             LOG.debug( "MembershipContentRule: Resource of type " + porttype.toString()
118                                 + " is member of service group. (MembershipInterface: "
119                                 + memberInterface.toString() + "). Resource will be added." );
120                         }
121                         else
122                         {
123                             LOG.debug( "MembershipContentRule: Resource of type " + porttype.toString()
124                                 + " not is member of service group. (MembershipInterface: "
125                                 + memberInterface.toString() + "). Resource was not added." );
126                         }
127 
128                     }
129 
130                     return isMember;
131                 }
132                 else
133                 {
134                     if ( LOG.isDebugEnabled() )
135                     {
136                         LOG.debug( "MembershipContentRule: "
137                             + "Could not retrieve resource port type from MembershipContentRuleContext. "
138                             + "Port type is null." );
139                     }
140                 }
141 
142                 if ( LOG.isDebugEnabled() )
143                 {
144                     LOG.debug( "MembershipContentRule: Try to retrieve resource port type WS-MEX." );
145                 }
146 
147                 //
148                 // The resource to be added is a remote resource. We try to lookup the
149                 // required metadata via MEX.
150                 //
151                 try
152                 {
153                     MetadataExchangeClient mex = new MetadataExchangeClient( memberEPR );
154 
155                     Element[] response = mex.getMetadata( WsagConstants.WSAG4J_MEX_DIALECT );
156                     if ( response.length == 1 )
157                     {
158                         try
159                         {
160                             WSAG4JMetadataDocument metadata =
161                                 (WSAG4JMetadataDocument) XmlObject.Factory.parse( response[0] );
162                             porttype = metadata.getWSAG4JMetadata().getWSAG4JPortType();
163 
164                             return memberInterface.getNamespaceURI().equals( porttype.getNamespaceURI() )
165                                 && memberInterface.getLocalPart().equals( porttype.getLocalPart() );
166 
167                         }
168                         catch ( XmlException e )
169                         {
170                             //
171                             // the returned document is not a valid XML document
172                             //
173                             return false;
174                         }
175                         catch ( ClassCastException e )
176                         {
177                             //
178                             // the returned document is not a valid WSAG4JMetadataDocument
179                             //
180                             return false;
181                         }
182                     }
183 
184                     //
185                     // this is not a wsag4j instance
186                     //
187                     return false;
188                 }
189                 catch ( Exception e )
190                 {
191                     //
192                     // the MEX service is not configured or reachable
193                     //
194                     return false;
195                 }
196             }
197         }
198 
199         return result;
200     }
201 
202 }