Coverage report

  %line %branch
org.apache.jetspeed.container.window.impl.PortletWindowAccessorImpl
0% 
0% 

 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  * 
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.jetspeed.container.window.impl;
 18  
 
 19  
 import java.util.Iterator;
 20  
 import java.util.Set;
 21  
 
 22  
 import org.apache.commons.logging.Log;
 23  
 import org.apache.commons.logging.LogFactory;
 24  
 import org.apache.jetspeed.cache.PortletWindowCache;
 25  
 import org.apache.jetspeed.components.portletentity.PortletEntityAccessComponent;
 26  
 import org.apache.jetspeed.components.portletentity.PortletEntityNotGeneratedException;
 27  
 import org.apache.jetspeed.components.portletentity.PortletEntityNotStoredException;
 28  
 import org.apache.jetspeed.components.portletregistry.PortletRegistry;
 29  
 import org.apache.jetspeed.components.portletregistry.RegistryEventListener;
 30  
 import org.apache.jetspeed.container.window.FailedToCreateWindowException;
 31  
 import org.apache.jetspeed.container.window.FailedToRetrievePortletWindow;
 32  
 import org.apache.jetspeed.container.window.PortletWindowAccessor;
 33  
 import org.apache.jetspeed.factory.PortletFactory;
 34  
 import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
 35  
 import org.apache.jetspeed.om.common.portlet.MutablePortletEntity;
 36  
 import org.apache.jetspeed.om.common.portlet.PortletApplication;
 37  
 import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
 38  
 import org.apache.jetspeed.om.page.ContentFragment;
 39  
 import org.apache.jetspeed.om.window.impl.PortletWindowImpl;
 40  
 import org.apache.jetspeed.util.ArgUtil;
 41  
 import org.apache.pluto.om.entity.PortletEntity;
 42  
 import org.apache.pluto.om.window.PortletWindow;
 43  
 import org.apache.pluto.om.window.PortletWindowCtrl;
 44  
 
 45  
 /**
 46  
  * Portlet Window Accessor Implementation
 47  
  *
 48  
  * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
 49  
  * @version $Id: PortletWindowAccessorImpl.java,v 1.12 2005/04/29 14:01:57 weaver Exp $
 50  
  */
 51  
 public class PortletWindowAccessorImpl implements PortletWindowAccessor, RegistryEventListener
 52  
 {
 53  0
     protected final static Log log = LogFactory.getLog(PortletWindowAccessorImpl.class);
 54  
    
 55  
     //private Map windows = Collections.synchronizedMap(new HashMap());    
 56  
     private PortletEntityAccessComponent entityAccessor;
 57  
     private PortletFactory portletFactory;
 58  0
     private boolean validateWindows = false;
 59  
     private PortletWindowCache portletWindowCache;
 60  
    
 61  
     
 62  
 
 63  
     public PortletWindowAccessorImpl(PortletEntityAccessComponent entityAccessor, PortletFactory portletFactory, PortletWindowCache portletWindowCache, boolean validateWindows)
 64  0
     {
 65  0
         this.entityAccessor = entityAccessor;
 66  0
         this.portletFactory = portletFactory;
 67  0
         this.validateWindows = validateWindows;
 68  0
         this.portletWindowCache = portletWindowCache;
 69  
 
 70  0
     }
 71  
 
 72  
     public PortletWindowAccessorImpl(PortletEntityAccessComponent entityAccessor, 
 73  
                                      PortletFactory portletFactory, 
 74  
                                      PortletRegistry registry, 
 75  
                                      PortletWindowCache portletWindowCache,
 76  
                                      boolean validateWindows)
 77  0
     {
 78  0
         this.entityAccessor = entityAccessor;
 79  0
         this.portletFactory = portletFactory;
 80  0
         this.validateWindows = validateWindows;
 81  0
         this.portletWindowCache = portletWindowCache;
 82  0
         registry.addRegistryListener(this);
 83  0
     }
 84  
     
 85  
     public PortletWindow createPortletWindow(PortletEntity entity, String windowId)
 86  
     {
 87  0
         if(entity == null)
 88  
         {
 89  0
             throw new IllegalArgumentException("cratePortletWindow requires a non-null PortletEntity");
 90  
         }
 91  
         
 92  0
         PortletWindow found = getWindowFromCache(windowId);
 93  0
         if (found != null)
 94  
         {
 95  
             // remove from cache if invalid entity
 96  0
             checkPortletWindowEntity(found);
 97  0
             ((PortletWindowCtrl)found).setPortletEntity(entity);
 98  0
             return found;
 99  
         }
 100  
         
 101  0
         PortletWindowImpl window = new PortletWindowImpl(windowId);
 102  0
         window.setPortletEntity(entity);
 103  0
         if ( isValidPortletEntity(entity))
 104  
         {
 105  
             // windows.put(windowId, window);
 106  0
         	portletWindowCache.putPortletWindow(window);
 107  
         }
 108  0
         return window;        
 109  
     }
 110  
 
 111  
     public PortletWindow createPortletWindow(String windowId)
 112  
     {
 113  0
         PortletWindow found = getWindowFromCache(windowId);
 114  0
         if (found != null)
 115  
         {
 116  
             // remove from cache if invalid entity
 117  0
             checkPortletWindowEntity(found);
 118  0
             return found;
 119  
         }        
 120  0
         PortletWindowImpl window = new PortletWindowImpl(windowId);
 121  0
         return window;                
 122  
     }
 123  
     
 124  
     public PortletWindow getPortletWindow(String windowId)
 125  
     {
 126  0
         PortletWindow window = getWindowFromCache(windowId);
 127  0
         if (window != null)
 128  
         {
 129  
             // remove from cache if invalid entity
 130  0
             checkPortletWindowEntity(window);
 131  
         }        
 132  0
         return window;
 133  
     }
 134  
     
 135  
     public PortletWindow getPortletWindow(ContentFragment fragment) throws FailedToRetrievePortletWindow, PortletEntityNotStoredException
 136  
     {
 137  0
         ArgUtil.assertNotNull(ContentFragment.class, fragment, this, "getPortletWindow(Fragment fragment)");
 138  0
         PortletWindow portletWindow = getWindowFromCache(fragment);        
 139  0
         if (portletWindow == null || !checkPortletWindowEntity(portletWindow))
 140  
         {
 141  
             try
 142  
             {
 143  0
                 return createPortletWindow(fragment);
 144  
             }
 145  0
             catch (FailedToCreateWindowException e)
 146  
             {
 147  0
                 throw new FailedToRetrievePortletWindow(e.toString(), e);
 148  
             }
 149  
         }
 150  
         else
 151  
         {
 152  0
             if (validateWindows)
 153  
             {
 154  0
                 validateWindow(fragment, portletWindow);
 155  
             }
 156  
         }
 157  
         
 158  0
         return portletWindow;
 159  
     }
 160  
     
 161  
     /**
 162  
      * <p>
 163  
      * validateWindow
 164  
      * </p>
 165  
      *
 166  
      * @param fragment
 167  
      * @param portletWindow
 168  
      * @throws PortletEntityNotStoredException 
 169  
      * @throws InconsistentWindowStateException
 170  
      */
 171  
     protected void validateWindow( ContentFragment fragment, PortletWindow portletWindow ) throws FailedToRetrievePortletWindow, PortletEntityNotStoredException
 172  
     {
 173  
         // make sure the window has the most up-to-date portlet entity
 174  0
         PortletEntity portletEntity = entityAccessor.getPortletEntityForFragment(fragment);
 175  0
         if(portletEntity != null)
 176  
         {
 177  0
             ((PortletWindowCtrl) portletWindow).setPortletEntity(portletEntity);
 178  
             // if not a valid entity, remove window from cache
 179  0
             checkPortletWindowEntity(portletWindow);
 180  
         }
 181  
         else
 182  
         {
 183  0
             removeWindow(portletWindow);  
 184  0
             throw new FailedToRetrievePortletWindow("No PortletEntity exists for for id "+fragment.getId()+" removing window from cache.");
 185  
         }
 186  0
     }
 187  
 
 188  
     public PortletWindow getPortletWindow(ContentFragment fragment, String principal) throws FailedToRetrievePortletWindow, FailedToCreateWindowException, PortletEntityNotStoredException
 189  
     {
 190  0
         ArgUtil.assertNotNull(ContentFragment.class, fragment, this, "getPortletWindow(Fragment fragment, String principal)");
 191  0
         ArgUtil.assertNotNull(String.class, principal, this, "getPortletWindow(Fragment fragment, String principal)");
 192  0
         PortletWindow portletWindow = getWindowFromCache(fragment);
 193  0
         if (portletWindow == null)
 194  
         {
 195  0
             return createPortletWindow(fragment, principal);
 196  
         }        
 197  
         else
 198  
         {
 199  
             // make sure the window has the most up-to-date portlet entity
 200  0
             validateWindow(fragment, portletWindow);
 201  
         }
 202  0
         return portletWindow;
 203  
     }
 204  
 
 205  
     private PortletWindow createPortletWindow(ContentFragment fragment) throws FailedToCreateWindowException, PortletEntityNotStoredException
 206  
     {
 207  0
         return createPortletWindow(fragment, null);
 208  
     }
 209  
     
 210  
     private PortletWindow createPortletWindow(ContentFragment fragment, String principal) throws FailedToCreateWindowException, PortletEntityNotStoredException
 211  
     {        
 212  0
         PortletWindow portletWindow = new PortletWindowImpl(fragment.getId());
 213  0
         boolean temporaryWindow = false;
 214  
                 
 215  0
         MutablePortletEntity portletEntity = entityAccessor.getPortletEntityForFragment(fragment, principal);
 216  0
         if (portletEntity == null)
 217  
         {
 218  0
             log.info("No portlet entity defined for fragment ID "+fragment.getId()+" attempting to auto-generate...");
 219  
             try
 220  
             {
 221  0
                 portletEntity = entityAccessor.generateEntityFromFragment(fragment, principal);
 222  
                 // not portlet definition most likely means that the portlet has not been deployed so dont worry about storing off the entity
 223  0
                 if(isValidPortletEntity(portletEntity))
 224  
                 {
 225  0
                     entityAccessor.storePortletEntity(portletEntity);
 226  
                 }
 227  
                 else
 228  
                 {
 229  
                     // don't cache the incomplete window
 230  0
                     temporaryWindow = true;
 231  
                 }
 232  
             }
 233  0
             catch (PortletEntityNotGeneratedException e)
 234  
             {
 235  0
                 throw new FailedToCreateWindowException("Error generating new PortletEntity: "+e.toString(), e);                
 236  
             }
 237  0
             catch (PortletEntityNotStoredException e)
 238  
             {
 239  0
                 throw new FailedToCreateWindowException("Error storing new PortletEntity: "+e.toString(), e);
 240  0
             }
 241  
             
 242  0
             if(portletEntity == null)
 243  
             {
 244  0
                 throw new FailedToCreateWindowException("Unable to generate portlet entity.");
 245  
             }
 246  
             
 247  
         }
 248  0
         ((PortletWindowCtrl) portletWindow).setPortletEntity(portletEntity);
 249  
         
 250  0
         if ( !temporaryWindow )
 251  
         {
 252  0
             portletWindowCache.putPortletWindow(portletWindow);
 253  
         }
 254  
         
 255  0
         return portletWindow;
 256  
     }
 257  
     
 258  
 
 259  
     public void removeWindows(PortletEntity portletEntity)
 260  
     {
 261  
 //        List tmpWindows = new ArrayList(windows.entrySet());
 262  
 //        for(int i = 0; i < tmpWindows.size(); i++)
 263  
 //        {
 264  
 //            PortletWindow window = (PortletWindow)((Map.Entry)tmpWindows.get(i)).getValue();
 265  
 //            if (portletEntity.getId().equals(window.getPortletEntity().getId()))
 266  
 //            {
 267  
 //                removeWindow(window);
 268  
 //            }
 269  
 //        }        
 270  
 //        tmpWindows.clear();
 271  
 //        
 272  0
         portletWindowCache.removePortletWindowByPortletEntityId(portletEntity.getId().toString());
 273  
 
 274  0
     }
 275  
     
 276  
     public void removeWindow(PortletWindow window)
 277  
     {
 278  
        // windows.remove(window.getId().toString());
 279  0
     	portletWindowCache.removePortletWindow(window.getId().toString());
 280  0
     }
 281  
     
 282  
     private PortletWindow getWindowFromCache(ContentFragment fragment)
 283  
     {
 284  0
         return portletWindowCache.getPortletWindow(fragment.getId());
 285  
     }
 286  
     
 287  
     private PortletWindow getWindowFromCache(String id)
 288  
     {
 289  0
         return portletWindowCache.getPortletWindow(id);
 290  
     }
 291  
 
 292  
     private boolean checkPortletWindowEntity(PortletWindow window)
 293  
     {
 294  0
         if (!isValidPortletEntity(window.getPortletEntity()))
 295  
         {
 296  0
             removeWindow(window);
 297  0
             return false;
 298  
         }
 299  0
         return true;
 300  
     }
 301  
     
 302  
     private boolean isValidPortletEntity(PortletEntity pe)
 303  
     {
 304  0
         return pe != null
 305  
                 && pe.getPortletDefinition() != null
 306  
                 && pe.getPortletDefinition().getPortletApplicationDefinition() != null
 307  
                 && portletFactory.isPortletApplicationRegistered((PortletApplication) pe.getPortletDefinition()
 308  
                         .getPortletApplicationDefinition());
 309  
     }
 310  
     
 311  
     public Set getPortletWindows()
 312  
     {
 313  0
         return portletWindowCache.getAllPortletWindows();
 314  
     }
 315  
 
 316  
     protected void removeForPortletDefinition(PortletDefinitionComposite def)
 317  
     {
 318  
 //        List tmpWindows = new ArrayList(windows.entrySet());
 319  
 //        for (int i = 0; i < tmpWindows.size(); i++)
 320  
 //        {
 321  
 //            PortletWindow window = (PortletWindow)((Map.Entry)tmpWindows.get(i)).getValue();
 322  
 //            PortletDefinitionComposite windowDef = (PortletDefinitionComposite)window.getPortletEntity().getPortletDefinition();            
 323  
 //            if(def != null && windowDef != null && def.getUniqueName() != null && def.getUniqueName().equals(windowDef.getUniqueName()))
 324  
 //            {
 325  
 //                removeWindow(window);
 326  
 //            }
 327  
 //        }        
 328  
 //        tmpWindows.clear(); 
 329  
 //        if (def != null)
 330  
 //            portletFactory.updatePortletConfig(def);
 331  
         
 332  
         
 333  0
         Iterator windows  = getPortletWindows().iterator();
 334  0
         while(windows.hasNext())
 335  
         {
 336  0
         	PortletWindow window = (PortletWindow) windows.next();
 337  0
         	PortletDefinitionComposite windowDef = (PortletDefinitionComposite)window.getPortletEntity().getPortletDefinition();            
 338  0
             if(def != null && windowDef != class="keyword">null && def.getUniqueName() != class="keyword">null && def.getUniqueName().equals(windowDef.getUniqueName()))
 339  
             {
 340  0
                 removeWindow(window);
 341  
             }
 342  0
         }
 343  
         
 344  0
         if (def != null)
 345  0
             portletFactory.updatePortletConfig(def);
 346  0
     }
 347  
 
 348  
     protected void removeForPortletApplication(MutablePortletApplication app)
 349  
     {
 350  
 //        List tmpWindows = new ArrayList(windows.entrySet());
 351  
 //        for (int i = 0; i < tmpWindows.size(); i++)
 352  
 //        {
 353  
 //            PortletWindow window = (PortletWindow)((Map.Entry)tmpWindows.get(i)).getValue();
 354  
 //            PortletDefinitionComposite pd =  (PortletDefinitionComposite)window.getPortletEntity().getPortletDefinition();
 355  
 //            if (pd != null)
 356  
 //            {
 357  
 //                MutablePortletApplication windowApp = (MutablePortletApplication)pd.getPortletApplicationDefinition();            
 358  
 //                if (app.getName().equals(windowApp.getName()))
 359  
 //                {
 360  
 //                    removeWindow(window);
 361  
 //                }
 362  
 //            }
 363  
 //        }        
 364  
 //        tmpWindows.clear();   
 365  
         
 366  0
         Iterator windows  = getPortletWindows().iterator();
 367  0
         while(windows.hasNext())
 368  
         {
 369  0
         	PortletWindow window = (PortletWindow) windows.next();
 370  0
         	PortletDefinitionComposite pd =  (PortletDefinitionComposite)window.getPortletEntity().getPortletDefinition();            
 371  0
         	 if (pd != null)
 372  
              {
 373  0
                  MutablePortletApplication windowApp = (MutablePortletApplication)pd.getPortletApplicationDefinition();            
 374  0
                  if (app.getName().equals(windowApp.getName()))
 375  
                  {
 376  0
                      removeWindow(window);
 377  
                  }
 378  
              }
 379  0
         }
 380  
         
 381  
         
 382  
         
 383  0
     }
 384  
     
 385  
     public void applicationRemoved(MutablePortletApplication app)
 386  
     {
 387  0
         if (app == null)
 388  
         {
 389  
             //System.out.println("@@@ receiving APP REMOVED message with NULL");
 390  0
             return;
 391  
         }
 392  
         //System.out.println("@@@ receiving APP REMOVED message: " + app.getName());
 393  0
         removeForPortletApplication(app);
 394  0
     }
 395  
 
 396  
  
 397  
     public void applicationUpdated(MutablePortletApplication app)
 398  
     {
 399  0
         if (app == null)
 400  
         {
 401  
             //System.out.println("@@@ receiving APP UPDATED message with NULL");
 402  0
             return;
 403  
         }
 404  
         //System.out.println("@@@ receiving APP UPDATED message: " + app.getName()); 
 405  0
         removeForPortletApplication(app);
 406  0
     }
 407  
 
 408  
     public void portletRemoved(PortletDefinitionComposite def)
 409  
     {
 410  0
         if (def == null)
 411  
         {
 412  
             //System.out.println("@@@ receiving DEF REMOVED message with NULL");
 413  0
             return;
 414  
         }
 415  
         //System.out.println("@@@ receiving DEF REMOVED message: " + def.getName()); 
 416  0
         removeForPortletDefinition(def);
 417  0
     }
 418  
  
 419  
     public void portletUpdated(PortletDefinitionComposite def)
 420  
     {
 421  0
         if (def == null)
 422  
         {
 423  
             //System.out.println("@@@ receiving DEF UPDATED message with NULL");
 424  0
             return;
 425  
         }
 426  
         //System.out.println("@@@ receiving DEF UPDATED message: " + def.getName());
 427  0
         removeForPortletDefinition(def);
 428  0
     }
 429  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.