Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
0 Kudos
1,169

Imagine a situation where you want to copy a file from a simple eclipse plugin project into local system as soon as the eclipse workbench  opens or when the software containing the said plugin is being installed. One way to achieve this task is to use org.eclipse.ui.startup extension. This blog explains how to use this extension and to copy file from the plugin project into local system.

Eclipse plugins are loaded whenever the user needs their functionalities. This is called lazy plug-in activation. The extension org.eclipse.ui.startup is designed to break this behavior. It forces the workbench to activate the plug-in as soon as the workbench window opens. And this extension should be used only in exceptional situations as it can  affect start-up performance.

Note that wherever I used the term code it meant Java code.

Do the following steps to copy a file attached with a plugin project into the local system.

  1. Create a plugin project, say demo.startup in eclipse IDE.
  2. Make sure that you have org.eclipse.ui as dependency.
  3. Add the extension, org.eclipse.ui.IStartup.
  4. Remove the entry Bundle-ActivationPolicy: lazy from MANIFEST.MF.
  5. Attach the file, say settings.xml, which you want to copy to the project.
  6. Create a package, say demo.startup.action under src folder.
  7. Create a java class, say FileCopier.

The FileCopier class should implement the interface IStartup (org.eclipse.ui.IStartup) and should override the method, earlyStartup() as given below.

import org.eclipse.ui.IStartup;

public class FileCopier implements IStartup {

     @Override

     public void earlyStartup() {           

          <Java code to copy file into local system during start up>

     }

}

Note that whenever the workbench opens, the plugin is loaded immediately and  earlyStartup() method is called automatically.

Now open the plugin.xml file and insert your startup class name as given below.

<plugin>

   <extension

         point="org.eclipse.ui.startup">

         <startup class="demo.startup.action.FileCopier"/>

   </extension>

</plugin>

Copying the file

Read the source  file (i.e. settings.xml) from the project

InputStream  inputStream =  this.getClass().getClassLoader().getResourceAsStream ("settings.xml");

Create a local file (i.e. target file) where you want to store the content of settings.xml.

File targetFile = new File(“C:/temp/settings.xml”);

Before copying make sure that the target file already exists. If exists check if the contents are same. If same, do not copy. Otherwise either overwrite or make a copy of existing file and then do copying. I prefer the second approach.

if(targetFile.exists()){

            InputStream targetInputStream = new FileInputStream(targetFile);

            if(contentEquals(inputStream,targetInputStream)){

                        return;

            }

            inputStream.close();

            targetInputStream.close();

            renameFile(targetFile);

            inputStream = getClass().getResourceAsStream("settings.xml");

}

            FileOutputStream outputStream;

            outputStream = new FileOutputStream(targetFile);

            byte[] buf = new byte[1024];

            int i = 0;

            while((i=inputStream.read(buf))!=-1) {

                        outputStream.write(buf, 0, i);

            }

            inputStream.close();

            outputStream.close();

Here is the code to check if the contents of the files are equal.

private boolean contentEquals(InputStream inputStream, InputStream targetInputStream) throws IOException {

                        if (!(inputStream instanceof BufferedInputStream)) {

                                    inputStream = new BufferedInputStream(inputStream);

                        }

                        if (!(targetInputStream instanceof BufferedInputStream)) {

                                    targetInputStream = new BufferedInputStream(targetInputStream);

                        }

                        int ch = inputStream.read();

                        while (-1 != ch) {

                                    int ch2 = targetInputStream.read();

                                    if (ch != ch2) {

                                                return false;

                                    }

                                    ch = inputStream.read();

                        }

                        int ch2 = targetInputStream.read();

                        return (ch2 == -1);

}

Here is the code to rename the existing file.

private void renameFile(File targetFile) throws IOException {

      for(int i=1;;i++){

          String newName = targetFile.getName().substring(0, targetFile.getName().lastIndexOf("."));

          newName = newName.concat(" - Copy ("+i+")"+".xml");

          File file = new File(targetFile.getParent()+File.separator+newName);

          if(file.exists()){

               continue;

         }

        FileInputStream inputStream = new FileInputStream(targetFile);

        FileOutputStream outputStream = new FileOutputStream(file);

        byte[] buf = new byte[1024];

        i = 0;

        while((i=inputStream.read(buf))!=-1) {

            outputStream.write(buf, 0, i);

        }

        break;

    }

}

Note: Catch the exceptions wherever required.

Restart the eclipse IDE. The file must be copied into the specified location in your local system. In case the file already exists and the content is not the same as that of the new file then it will create a copy like settings - Copy.xml. If settings – copy.xml file also exists then it will create settings - Copy (2).xml and so on.

1 Comment