Java tutorial
/*---------------- FILE HEADER KALYPSO ------------------------------------------ * * This file is part of kalypso. * Copyright (C) 2004 by: * * Technical University Hamburg-Harburg (TUHH) * Institute of River and coastal engineering * Denickestrae 22 * 21073 Hamburg, Germany * http://www.tuhh.de/wb * * and * * Bjoernsen Consulting Engineers (BCE) * Maria Trost 3 * 56070 Koblenz, Germany * http://www.bjoernsen.de * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Contact: * * E-Mail: * belger@bjoernsen.de * schlienger@bjoernsen.de * v.doemming@tuhh.de * * ---------------------------------------------------------------------------*/ package org.kalypso.project.database.client.core.base.worker; import java.io.File; import java.net.URL; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.vfs2.FileObject; import org.apache.commons.vfs2.FileSystemManager; import org.apache.commons.vfs2.cache.OnCallRefreshFileObject; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProjectDescription; import org.eclipse.core.resources.WorkspaceJob; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.kalypso.commons.io.VFSUtilities; import org.kalypso.contribs.eclipse.core.runtime.StatusUtilities; import org.kalypso.contribs.eclipse.jface.operation.ICoreRunnableWithProgress; import org.kalypso.module.project.local.ILocalProject; import org.kalypso.module.project.local.wizard.export.ProjectExportWorker; import org.kalypso.project.database.client.KalypsoProjectDatabaseClient; import org.kalypso.project.database.client.extension.database.IProjectDataBaseClientConstant; import org.kalypso.project.database.client.i18n.Messages; import org.kalypso.project.database.common.nature.IRemoteProjectPreferences; import org.kalypso.project.database.common.nature.RemoteProjectNature; import org.kalypso.project.database.common.utils.ProjectModelUrlResolver; import org.kalypso.project.database.sei.IProjectDatabase; import org.kalypso.project.database.sei.beans.KalypsoProjectBean; /** * @author Dirk Kuch */ public class CreateRemoteProjectWorker implements ICoreRunnableWithProgress { private final ILocalProject m_handler; private final String m_commitType; public CreateRemoteProjectWorker(final String commitType, final ILocalProject handler) { m_commitType = commitType; m_handler = handler; } /** * @see org.kalypso.contribs.eclipse.jface.operation.ICoreRunnableWithProgress#execute(org.eclipse.core.runtime.IProgressMonitor) */ @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; } }