org.symbian.tools.mtw.ui/src/org/symbian/tools/mtw/internal/deployment/targets/FilesystemDeploymentTarget.java
changeset 461 7a8f9fa8d278
parent 460 c0bff5ed874c
child 462 cdc4995b1677
equal deleted inserted replaced
460:c0bff5ed874c 461:7a8f9fa8d278
     1 /**
       
     2  * Copyright (c) 2010 Symbian Foundation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Symbian Foundation - initial contribution.
       
    11  * Contributors:
       
    12  * Description:
       
    13  * Overview:
       
    14  * Details:
       
    15  * Platforms/Drives/Compatibility:
       
    16  * Assumptions/Requirement/Pre-requisites:
       
    17  * Failures and causes:
       
    18  */
       
    19 package org.symbian.tools.mtw.internal.deployment.targets;
       
    20 
       
    21 import java.io.BufferedInputStream;
       
    22 import java.io.BufferedOutputStream;
       
    23 import java.io.File;
       
    24 import java.io.FileInputStream;
       
    25 import java.io.FileOutputStream;
       
    26 import java.io.IOException;
       
    27 import java.io.InputStream;
       
    28 import java.io.OutputStream;
       
    29 
       
    30 import org.eclipse.core.runtime.CoreException;
       
    31 import org.eclipse.core.runtime.IPath;
       
    32 import org.eclipse.core.runtime.IProgressMonitor;
       
    33 import org.eclipse.core.runtime.IStatus;
       
    34 import org.eclipse.core.runtime.Path;
       
    35 import org.eclipse.core.runtime.PlatformObject;
       
    36 import org.eclipse.core.runtime.Status;
       
    37 import org.eclipse.core.runtime.jobs.ISchedulingRule;
       
    38 import org.eclipse.ui.IMemento;
       
    39 import org.symbian.tools.mtw.core.projects.IMTWProject;
       
    40 import org.symbian.tools.mtw.core.runtimes.IPackager;
       
    41 import org.symbian.tools.mtw.ui.MTWCoreUI;
       
    42 import org.symbian.tools.mtw.ui.deployment.IDeploymentTarget;
       
    43 import org.symbian.tools.mtw.ui.deployment.IDeploymentTargetType;
       
    44 
       
    45 public class FilesystemDeploymentTarget extends PlatformObject implements IDeploymentTargetType, IDeploymentTarget {
       
    46     private String defaultName;
       
    47     private IPath path;
       
    48 
       
    49     public IStatus deploy(IMTWProject project, IPackager packager, IProgressMonitor monitor) throws CoreException {
       
    50         final File file = packager.packageApplication(project, monitor);
       
    51         try {
       
    52             final InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
       
    53             final OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(path.toFile()));
       
    54             try {
       
    55                 final byte[] buffer = new byte[16384];
       
    56                 int read = -1;
       
    57                 while ((read = inputStream.read(buffer)) > 0) {
       
    58                     outputStream.write(buffer, 0, read);
       
    59                 }
       
    60             } finally {
       
    61                 inputStream.close();
       
    62                 outputStream.close();
       
    63             }
       
    64         } catch (IOException e) {
       
    65             throw new CoreException(
       
    66                     new Status(IStatus.ERROR, MTWCoreUI.PLUGIN_ID, "Failed to copy application file", e));
       
    67         } finally {
       
    68             file.delete();
       
    69         }
       
    70         return Status.OK_STATUS;
       
    71     }
       
    72 
       
    73     public void discoverTargets(IProgressMonitor monitor) throws CoreException {
       
    74         // Do nothing
       
    75     }
       
    76 
       
    77     public IDeploymentTarget findTarget(IMTWProject project, String id) {
       
    78         return getId().equals(id) ? this : null;
       
    79     }
       
    80 
       
    81     public String getDefaultName() {
       
    82         return defaultName;
       
    83     }
       
    84 
       
    85     public String getDescription() {
       
    86         return "Copies application package to a location in the local filesystem";
       
    87     }
       
    88 
       
    89     public String getId() {
       
    90         return "mtw.filesystem";
       
    91     }
       
    92 
       
    93     public String getName() {
       
    94         return "Local filesystem";
       
    95     }
       
    96 
       
    97     public IPath getPath() {
       
    98         return path;
       
    99     }
       
   100 
       
   101     public ISchedulingRule getSchedulingRule(IDeploymentTarget target) {
       
   102         return null;
       
   103     }
       
   104 
       
   105     public IDeploymentTarget[] getTargets(IMTWProject project) {
       
   106         return new IDeploymentTarget[] { this };
       
   107     }
       
   108 
       
   109     public void init(IMTWProject project, IPackager packager, IMemento memento) {
       
   110         defaultName = new Path(project.getName()).addFileExtension(packager.getFileType(project)).toOSString();
       
   111         path = null;
       
   112         String string = memento != null ? memento.getString("path") : null;
       
   113         if (string == null) {
       
   114             string = MTWCoreUI.getDefault().getPreferenceStore().getString("path");
       
   115             if (string != null) {
       
   116                 final IPath p = Path.fromPortableString(string);
       
   117                 if (p.toFile().isDirectory()) {
       
   118                     this.path = p.append(defaultName);
       
   119                 }
       
   120             }
       
   121         } else {
       
   122             path = Path.fromPortableString(string);
       
   123             if (!path.removeLastSegments(1).toFile().isDirectory()) {
       
   124                 path = null;
       
   125             }
       
   126         }
       
   127     }
       
   128 
       
   129     public void save(IMemento memento) {
       
   130         memento.putString("path", path.toPortableString());
       
   131         MTWCoreUI.getDefault().getPreferenceStore().setValue("path", path.removeLastSegments(1).toPortableString());
       
   132     }
       
   133 
       
   134     public void setPath(IPath path) {
       
   135         this.path = path;
       
   136     }
       
   137 
       
   138     public boolean targetsDiscovered() {
       
   139         return true;
       
   140     }
       
   141 
       
   142 }