/*
** Apollo - Test Skeleton Toolkit for Web Start/JNLP
** Copyright (c) 2001, 2002, 2003 by Gerald Bauer
**
** This program is free software.
**
** You may redistribute it and/or modify it under the terms of the GNU
** General Public License as published by the Free Software Foundation.
** Version 2 of the license should be included with this distribution in
** the file LICENSE, as well as License.html. If the license is not
** included with this distribution, you may find a copy at the FSF web
** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE.
**
*/

package apollo.jnlp;

import apollo.*;
import apollo.spi.*;

public class JnlpServiceResolver implements ServiceResolver
{

   private BasicService _basic;
   private ClipboardService _clipboard;
   private ServiceResolver _fallback;
   private FileOpenService _fileOpen;
   private FileSaveService _fileSave;
   private PersistenceService _persistence;
   private PrintService _print;

   public JnlpServiceResolver( ServiceResolver fallback )
   {
      _fallback = fallback;
   }

   public BasicService lookupBasicService()
   {
      if( _basic == null )
      {
         try
         {
            _basic = new JnlpBasicService(
                  ( javax.jnlp.BasicService ) javax.jnlp.ServiceManager.lookup( Jnlp.Class.BASIC_SERVICE ) );
         }
         catch( javax.jnlp.UnavailableServiceException uex )
         {
            // todo: issue warning/error
            _basic = _fallback.lookupBasicService();
         }
      }
      return _basic;
   }

   public ClipboardService lookupClipboardService()
   {
      if( _clipboard == null )
      {
         try
         {
            _clipboard = new JnlpClipboardService(
                  ( javax.jnlp.ClipboardService ) javax.jnlp.ServiceManager.lookup( Jnlp.Class.CLIPBOARD_SERVICE ) );
         }
         catch( javax.jnlp.UnavailableServiceException uex )
         {
            // todo: issue warning/error
            _clipboard = _fallback.lookupClipboardService();
         }
      }
      return _clipboard;
   }

   public FileOpenService lookupFileOpenService()
   {
      if( _fileOpen == null )
      {
         try
         {
            _fileOpen = new JnlpFileOpenService(
                  ( javax.jnlp.FileOpenService ) javax.jnlp.ServiceManager.lookup( Jnlp.Class.FILE_OPEN_SERVICE ) );
         }
         catch( javax.jnlp.UnavailableServiceException uex )
         {
            // todo: issue warning/error
            _fileOpen = _fallback.lookupFileOpenService();
         }
      }
      return _fileOpen;
   }

   public FileSaveService lookupFileSaveService()
   {
      if( _fileSave == null )
      {
         try
         {
            _fileSave = new JnlpFileSaveService(
                  ( javax.jnlp.FileSaveService ) javax.jnlp.ServiceManager.lookup( Jnlp.Class.FILE_SAVE_SERVICE ) );
         }
         catch( javax.jnlp.UnavailableServiceException uex )
         {
            // todo: issue warning/error
            _fileSave = _fallback.lookupFileSaveService();
         }
      }
      return _fileSave;
   }

   public PersistenceService lookupPersistenceService()
   {
      if( _persistence == null )
      {
         try
         {
            _persistence = new JnlpPersistenceService(
                  ( javax.jnlp.PersistenceService ) javax.jnlp.ServiceManager.lookup( Jnlp.Class.PERSISTENCE_SERVICE ) );
         }
         catch( javax.jnlp.UnavailableServiceException uex )
         {
            // todo: issue warning/error
            _persistence = _fallback.lookupPersistenceService();
         }
      }
      return _persistence;
   }

   public PrintService lookupPrintService()
   {
      if( _print == null )
      {
         try
         {
            _print = new JnlpPrintService(
                  ( javax.jnlp.PrintService ) javax.jnlp.ServiceManager.lookup( Jnlp.Class.PRINT_SERVICE ) );
         }
         catch( javax.jnlp.UnavailableServiceException uex )
         {
            // todo: issue warning/error
            _print = _fallback.lookupPrintService();
         }
      }
      return _print;
   }
}