001    /*
002     * This file is part of the Echo Point Project.  This project is a
003     * collection of Components that have extended the Echo Web Application
004     * Framework Version 3.
005     *
006     * Version: MPL 1.1
007     *
008     * The contents of this file are subject to the Mozilla Public License Version
009     * 1.1 (the "License"); you may not use this file except in compliance with
010     * the License. You may obtain a copy of the License at
011     * http://www.mozilla.org/MPL/
012     *
013     * Software distributed under the License is distributed on an "AS IS" basis,
014     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
015     * for the specific language governing rights and limitations under the
016     * License.
017     */
018    package echopoint.tucana;
019    
020    import nextapp.echo.app.Command;
021    import nextapp.echo.app.util.Context;
022    import nextapp.echo.webcontainer.AbstractCommandSynchronizePeer;
023    import nextapp.echo.webcontainer.ServerMessage;
024    import nextapp.echo.webcontainer.Service;
025    import nextapp.echo.webcontainer.UserInstance;
026    import static nextapp.echo.webcontainer.WebContainerServlet.getServiceRegistry;
027    import nextapp.echo.webcontainer.service.JavaScriptService;
028    
029    import java.util.Map;
030    import java.util.concurrent.ConcurrentHashMap;
031    
032    import echopoint.internal.CommonService;
033    import echopoint.internal.CommonResources;
034    
035    /**
036     * Synchronisation peer for the {@link echopoint.tucana.DownloadCommand} command.
037     *
038     * @author Rakesh Vidyadharan 2008-11-10
039     * @version $Id: DownloadCommandPeer.java 218 2009-06-05 12:25:29Z sptrakesh $
040     */
041    public class DownloadCommandPeer extends AbstractCommandSynchronizePeer
042    {
043      private static final Map<String, DownloadCommand> ID_TO_DOWNLOAD_MAP =
044          new ConcurrentHashMap<String, DownloadCommand>();
045    
046      private static final Service DOWNLOAD_SERVICE =
047          JavaScriptService.forResource( DownloadCommand.class.getName(),
048              "resource/js/tucana/DownloadService.js" );
049    
050      static
051      {
052        CommonResources.install();
053        DownloadService.install();
054        getServiceRegistry().add( DOWNLOAD_SERVICE );
055      }
056    
057      public DownloadCommandPeer()
058      {
059        super();
060        addProperty( "uri", new AbstractCommandSynchronizePeer.PropertyPeer()
061        {
062          public Object getProperty( Context context, Command command )
063          {
064            final DownloadCommand download = (DownloadCommand) command;
065            final UserInstance userInstance = (UserInstance) context.get( UserInstance.class );
066            final String id = download.getRenderId();
067            ID_TO_DOWNLOAD_MAP.put( id, download );
068            return DownloadService.getInstance().createUri( userInstance, id );
069          }
070        } );
071      }
072    
073      @Override
074      public void init( Context context )
075      {
076        super.init( context );
077        ServerMessage serverMessage = (ServerMessage)
078            context.get( ServerMessage.class );
079        serverMessage.addLibrary( CommonService.ECHOPOINT_SERVICE.getId() );
080        serverMessage.addLibrary( DOWNLOAD_SERVICE.getId() );
081      }
082    
083      public Class getCommandClass()
084      {
085        return DownloadCommand.class;
086      }
087    
088      /**
089       * Returns the {@link echopoint.tucana.DownloadCommand} having the passed
090       * id, and removes it from the internal map.
091       *
092       * <p>This means that a particular download command
093       * cannot be re-used. A new download command must be created every time,
094       * e.g. each time your download button is clicked.</p>
095       *
096       * <p>This is necessary to prevent memory leaks.</p>
097       *
098       * @param id The download id.
099       * @return The {@link echopoint.tucana.DownloadCommand} instance.
100       */
101      public static DownloadCommand getAndRemoveDownload( String id )
102      {
103        return ID_TO_DOWNLOAD_MAP.remove( id );
104      }
105    }