View Javadoc
1   /*
2    * Copyright (c) 1997, 2012, 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.sun.xml.internal.ws.api.pipe;
27  
28  import java.util.HashMap;
29  import java.util.Map;
30  import java.util.logging.Logger;
31  import java.util.logging.Level;
32  
33  import com.sun.xml.internal.ws.api.pipe.helper.AbstractTubeImpl;
34  
35  /**
36   * Clones the whole pipeline.
37   *
38   * <p>
39   * Since {@link Pipe}s may form an arbitrary directed graph, someone needs
40   * to keep track of isomorphism for a clone to happen correctly. This class
41   * serves that role.
42   *
43   * @author Kohsuke Kawaguchi
44   */
45  @SuppressWarnings("deprecation")
46  public class PipeClonerImpl extends PipeCloner {
47  
48      private static final Logger LOGGER = Logger.getLogger(PipeClonerImpl.class.getName());
49  
50      // no need to be constructed publicly. always use the static clone method.
51      /*package*/ public PipeClonerImpl() {
52          super(new HashMap<Object,Object>());
53      }
54  
55      protected PipeClonerImpl(Map<Object,Object> master2copy) {
56          super(master2copy);
57      }
58  
59      /**
60       * {@link Pipe} version of {@link #copy(Tube)}
61       */
62      @SuppressWarnings("unchecked")
63          public <T extends Pipe> T copy(T p) {
64          Pipe r = (Pipe)master2copy.get(p);
65          if(r==null) {
66              r = p.copy(this);
67              // the pipe must puts its copy to the map by itself
68              assert master2copy.get(p)==r : "the pipe must call the add(...) method to register itself before start copying other pipes, but "+p+" hasn't done so";
69          }
70          return (T)r;
71      }
72  
73  
74      /**
75       * The {@link Pipe} version of {@link #add(Tube, Tube)}.
76       */
77      public void add(Pipe original, Pipe copy) {
78          assert !master2copy.containsKey(original);
79          assert original!=null && copy!=null;
80          master2copy.put(original,copy);
81      }
82  
83      /**
84       * Disambiguation version.
85       */
86      public void add(AbstractTubeImpl original, AbstractTubeImpl copy) {
87          add((Tube)original,copy);
88      }
89  
90          @Override
91          public void add(Tube original, Tube copy) {
92          assert !master2copy.containsKey(original);
93          assert original!=null && copy!=null;
94          master2copy.put(original,copy);
95          }
96  
97          @SuppressWarnings("unchecked")
98          @Override
99          public <T extends Tube> T copy(T t) {
100         Tube r = (Tube)master2copy.get(t);
101         if(r==null) {
102             if (t != null) {
103               r = t.copy(this);
104             } else {
105               if (LOGGER.isLoggable(Level.FINER)) {
106                 LOGGER.fine("WARNING, tube passed to 'copy' in " + this + " was null, so no copy was made");
107               }
108             }
109         }
110         return (T)r;
111         }
112 }