View Javadoc
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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.internal.component;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import javax.faces.context.FacesContext;
26  import javax.faces.event.AbortProcessingException;
27  import javax.faces.event.FacesEvent;
28  import java.io.IOException;
29  import java.util.Map;
30  
31  public abstract class AbstractUIMediator extends AbstractUIPanelBase {
32  
33    private static final Logger LOG = LoggerFactory.getLogger(AbstractUIMediator.class);
34  
35    private String var;
36  
37    @Override
38    public void encodeBegin(final FacesContext facesContext) throws IOException {
39      super.encodeBegin(facesContext);
40      if (var != null) {
41        facesContext.getExternalContext().getRequestMap().put(var, this);
42      }
43    }
44  
45    @Override
46    public void encodeEnd(final FacesContext facesContext) throws IOException {
47      if (var != null) {
48        facesContext.getExternalContext().getRequestMap().remove(var);
49      }
50      super.encodeEnd(facesContext);
51    }
52  
53    @Override
54    public void broadcast(final FacesEvent event) throws AbortProcessingException {
55      final Map requestMap = FacesContext.getCurrentInstance().getExternalContext().getRequestMap();
56      requestMap.put(var, this);
57      try {
58        super.broadcast(event);
59      } finally {
60        requestMap.remove(var);
61      }
62    }
63  
64    @Override
65    public Object saveState(final FacesContext context) {
66      final Object[] state = new Object[2];
67      state[0] = super.saveState(context);
68      state[1] = var;
69      return state;
70    }
71  
72    @Override
73    public void restoreState(final FacesContext context, final Object state) {
74      final Object[] values = (Object[]) state;
75      super.restoreState(context, values[0]);
76      var = (String) values[1];
77    }
78  
79    public String getVar() {
80      return var;
81    }
82  
83    public void setVar(final String var) {
84      this.var = var;
85    }
86  }