Example usage for org.apache.commons.vfs2.cache OnCallRefreshFileObject OnCallRefreshFileObject

List of usage examples for org.apache.commons.vfs2.cache OnCallRefreshFileObject OnCallRefreshFileObject

Introduction

In this page you can find the example usage for org.apache.commons.vfs2.cache OnCallRefreshFileObject OnCallRefreshFileObject.

Prototype

public OnCallRefreshFileObject(final FileObject fileObject) 

Source Link

Usage

From source file:org.kalypso.project.database.client.core.base.worker.CreateRemoteProjectWorker.java

/**
 * @see org.kalypso.contribs.eclipse.jface.operation.ICoreRunnableWithProgress#execute(org.eclipse.core.runtime.IProgressMonitor)
 *//*w  w w  .  j av  a 2  s.  com*/
@Override
public IStatus execute(final IProgressMonitor monitor) throws CoreException {

    final IProject project = m_handler.getProject();
    final String zipName = String.format("%s.zip", project.getName()); //$NON-NLS-1$

    final File urlTempDir = new File(System.getProperty("java.io.tmpdir")); //$NON-NLS-1$
    final File src = new File(urlTempDir, zipName); //$NON-NLS-1$

    try {
        final ProjectExportWorker worker = new ProjectExportWorker(project, src, false);
        final IStatus status = worker.execute(monitor);

        if (!status.isOK())
            throw new CoreException(
                    new Status(IStatus.ERROR, KalypsoProjectDatabaseClient.PLUGIN_ID, Messages.getString(
                            "org.kalypso.project.database.client.core.project.create.CreateRemoteProjectWorker.2"))); //$NON-NLS-1$

        final FileSystemManager manager = VFSUtilities.getManager();
        final FileObject source = manager.resolveFile(src.getAbsolutePath());

        final String urlDestination = ProjectModelUrlResolver
                .getUrlAsFtp(new ProjectModelUrlResolver.IResolverInterface() {
                    @Override
                    public String getPath() {
                        return System.getProperty(IProjectDataBaseClientConstant.SERVER_INCOMING_PATH);
                    }

                }, zipName); //$NON-NLS-1$

        // change caching strategy of destination file to avoid deadlocks!
        final FileObject destination = new OnCallRefreshFileObject(manager.resolveFile(urlDestination));

        VFSUtilities.copy(source, destination);

        final IProjectDatabase service = KalypsoProjectDatabaseClient.getService();

        // always commit - download of projects assert nature!
        final KalypsoProjectBean bean = new KalypsoProjectBean();
        bean.setName(project.getName());
        bean.setDescription(project.getName());
        bean.setUnixName(project.getName()); // TODO generate unixName
        bean.setProjectVersion(0);
        bean.setProjectType(m_commitType);
        bean.setModuleIdentifier(m_commitType);

        service.createProject(bean, new URL(urlDestination));

        final WorkspaceJob updateDescription = new WorkspaceJob("") //$NON-NLS-1$
        {
            int updateCount = 0;

            @Override
            public IStatus runInWorkspace(final IProgressMonitor m) throws CoreException {
                try {
                    // FIXME *grummel* why .project file is always locked and project.setDescription() fails?
                    final IProjectDescription description = project.getDescription();
                    description.setNatureIds(
                            ArrayUtils.add(description.getNatureIds(), RemoteProjectNature.NATURE_ID));
                    project.setDescription(description, m);

                    final RemoteProjectNature remote = (RemoteProjectNature) project
                            .getNature(RemoteProjectNature.NATURE_ID);
                    final IRemoteProjectPreferences preferences = remote.getRemotePreferences(project, null);
                    preferences.setVersion(0);
                    preferences.setIsOnServer(true);
                    preferences.setModified(false);
                } catch (final Exception e) {
                    updateCount += 1;
                    if (updateCount < 5)
                        this.schedule(250);
                    else
                        throw new CoreException(new Status(IStatus.ERROR,
                                KalypsoProjectDatabaseClient.PLUGIN_ID,
                                Messages.getString(
                                        "org.kalypso.project.database.client.core.project.create.CreateRemoteProjectWorker.0"), //$NON-NLS-1$
                                e));

                    return Status.CANCEL_STATUS;
                }

                return Status.OK_STATUS;
            }
        };

        updateDescription.schedule(250);

        // bad @hack if the client has committed a large file, it can happen, that the client looses the http connection.
        // file.close() reestablish this http-connection
        destination.close();
    } catch (final Exception e) {
        throw new CoreException(StatusUtilities.statusFromThrowable(e));
    } finally {
        src.delete();
    }

    return Status.OK_STATUS;
}