View Javadoc
1   /*
2    * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package com.oracle.webservices.internal.api.databinding;
27  
28  import com.sun.xml.internal.ws.api.databinding.MetadataReader;
29  import com.sun.xml.internal.ws.model.ExternalMetadataReader;
30  
31  import javax.xml.ws.WebServiceFeature;
32  import java.io.File;
33  import java.util.ArrayList;
34  import java.util.Collections;
35  import java.util.List;
36  
37  /**
38   * WebServiceFeature allowing to define either on server or client side external xml descriptors replacing/supplementing
39   * WS metadata provided by class annotations. This can be useful if those annotations are missing (existing non-WS
40   * components) or if it is necessary to override those.
41   *
42   * @author Miroslav Kos (miroslav.kos at oracle.com)
43   */
44  public class ExternalMetadataFeature extends WebServiceFeature {
45  
46      private static final String ID = "com.oracle.webservices.internal.api.databinding.ExternalMetadataFeature";
47  
48      /**
49       * Enable this feature.  Defaults to true.
50       */
51      private boolean enabled = true;
52  
53      private List<String> resourceNames;
54      private List<File> files;
55      private MetadataReader reader;
56  
57      private ExternalMetadataFeature() {
58      }
59  
60      public void addResources(String... resourceNames) {
61          if (this.resourceNames == null) {
62              this.resourceNames = new ArrayList<String>();
63          }
64          Collections.addAll(this.resourceNames, resourceNames);
65      }
66  
67      public List<String> getResourceNames() { return resourceNames; }
68  
69      public void addFiles(File... files) {
70          if (this.files == null) {
71              this.files = new ArrayList<File>();
72          }
73          Collections.addAll(this.files, files);
74      }
75  
76      public List<File> getFiles() { return files; }
77  
78      public boolean isEnabled() {
79          return enabled;
80      }
81  
82      private void setEnabled(final boolean x) {
83          enabled = x;
84      }
85  
86      @Override
87      public String getID() {
88          return ID;
89      }
90  
91      public MetadataReader getMetadataReader(ClassLoader classLoader, boolean disableXmlSecurity) {
92          if (reader != null && enabled) return reader;
93          return enabled ? new ExternalMetadataReader(files, resourceNames, classLoader, true, disableXmlSecurity) : null;
94      }
95  
96      @Override
97      public boolean equals(Object o) {
98          if (this == o) return true;
99          if (o == null || getClass() != o.getClass()) return false;
100 
101         ExternalMetadataFeature that = (ExternalMetadataFeature) o;
102 
103         if (enabled != that.enabled) return false;
104         if (files != null ? !files.equals(that.files) : that.files != null) return false;
105         if (resourceNames != null ? !resourceNames.equals(that.resourceNames) : that.resourceNames != null)
106             return false;
107 
108         return true;
109     }
110 
111     @Override
112     public int hashCode() {
113         int result = (enabled ? 1 : 0);
114         result = 31 * result + (resourceNames != null ? resourceNames.hashCode() : 0);
115         result = 31 * result + (files != null ? files.hashCode() : 0);
116         return result;
117     }
118 
119     @Override
120     public String toString() {
121         return "[" + getID() +
122                 ", enabled=" + enabled +
123                 ", resourceNames=" + resourceNames +
124                 ", files=" + files +
125                 ']';
126     }
127 
128     public static Builder builder() {
129         return new Builder(new ExternalMetadataFeature());
130     }
131 
132     public final static class Builder {
133         final private ExternalMetadataFeature o;
134 
135         Builder(final ExternalMetadataFeature x) {
136             o = x;
137         }
138 
139         public ExternalMetadataFeature build() {
140             return o;
141         }
142 
143         public Builder addResources(String... res) {
144             o.addResources(res);
145             return this;
146         }
147 
148         public Builder addFiles(File... files) {
149             o.addFiles(files);
150             return this;
151         }
152 
153         public Builder setEnabled(boolean enabled) {
154             o.setEnabled(enabled);
155             return this;
156         }
157 
158         public Builder setReader( MetadataReader r ) {
159             o.reader = r;
160             return this;
161         }
162     }
163 }