1 /*******************************************************************************
\r
2 * Copyright (c) 2013 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.graphfile.hack;
\r
14 import java.io.File;
\r
15 import java.io.FileFilter;
\r
16 import java.io.FilenameFilter;
\r
17 import java.io.IOException;
\r
18 import java.net.MalformedURLException;
\r
19 import java.net.URI;
\r
20 import java.net.URL;
\r
22 import org.eclipse.core.runtime.IStatus;
\r
23 import org.eclipse.core.runtime.Status;
\r
24 import org.simantics.Simantics;
\r
25 import org.simantics.db.ReadGraph;
\r
26 import org.simantics.db.Resource;
\r
27 import org.simantics.db.Session;
\r
28 import org.simantics.db.WriteGraph;
\r
29 import org.simantics.db.common.request.WriteRequest;
\r
30 import org.simantics.db.exception.DatabaseException;
\r
31 import org.simantics.db.request.Read;
\r
32 import org.simantics.graphfile.Activator;
\r
33 import org.simantics.graphfile.ontology.GraphFileResource;
\r
34 import org.simantics.layer0.Layer0;
\r
37 * This is a wrapper for Graph based files.
\r
39 * DO NOT USE this with File(Input/Output)Stream.
\r
41 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
44 public class GraphJavaFile extends File {
\r
46 private static final long serialVersionUID = 8213749101292672725L;
\r
47 private Session session;
\r
48 private Resource fileResource;
\r
50 public GraphJavaFile(Resource fileResource) {
\r
52 this.fileResource = fileResource;
\r
53 this.session = Simantics.getSession();
\r
56 private byte[] getResourceData(ReadGraph graph) throws DatabaseException {
\r
57 GraphFileResource gf = GraphFileResource.getInstance(graph);
\r
58 return graph.getRelatedValue(fileResource, gf.HasFiledata);
\r
61 private byte[] getResourceData() throws DatabaseException {
\r
62 return Simantics.getSession().syncRequest(new Read<byte[]>() {
\r
64 public byte[] perform(ReadGraph graph) throws DatabaseException {
\r
65 return getResourceData(graph);
\r
71 public boolean canExecute() {
\r
76 public boolean canRead() {
\r
81 public boolean canWrite() {
\r
86 public boolean createNewFile() throws IOException {
\r
91 public boolean delete() {
\r
93 session.syncRequest(new WriteRequest() {
\r
96 public void perform(WriteGraph graph) throws DatabaseException {
\r
97 graph.deny(fileResource);
\r
101 } catch (DatabaseException e) {
\r
102 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to delete file resource " + fileResource, e));
\r
108 public void deleteOnExit() {
\r
113 public boolean equals(Object obj) {
\r
116 if (!obj.getClass().equals(getClass()))
\r
118 GraphJavaFile other = (GraphJavaFile)obj;
\r
119 return fileResource.equals(other.fileResource);
\r
123 public File getAbsoluteFile() {
\r
128 public boolean exists() {
\r
130 return session.syncRequest(new Read<Boolean>() {
\r
132 public Boolean perform(ReadGraph graph) throws DatabaseException {
\r
133 GraphFileResource gf = GraphFileResource.getInstance(graph);
\r
134 return graph.getPossibleRelatedValue(fileResource, gf.HasFiledata) != null;
\r
137 } catch (DatabaseException e) {
\r
138 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "exists check failed for file resource " + fileResource, e));
\r
144 public String getAbsolutePath() {
\r
149 public File getCanonicalFile() throws IOException {
\r
154 public String getCanonicalPath() throws IOException {
\r
159 public long getFreeSpace() {
\r
164 public String getName() {
\r
166 return Simantics.getSession().syncRequest(new Read<String>() {
\r
168 public String perform(ReadGraph graph) throws DatabaseException {
\r
169 Layer0 l0 = Layer0.getInstance(graph);
\r
170 return graph.getRelatedValue(fileResource, l0.HasName);
\r
173 } catch (DatabaseException e) {
\r
174 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "getName failed for file resource " + fileResource, e));
\r
175 return e.getMessage();
\r
180 public String getParent() {
\r
185 public File getParentFile() {
\r
190 public String getPath() {
\r
195 public long getTotalSpace() {
\r
200 public long getUsableSpace() {
\r
205 public boolean isAbsolute() {
\r
210 public boolean isDirectory() {
\r
215 public boolean isFile() {
\r
220 public int hashCode() {
\r
221 return fileResource.hashCode();
\r
225 public boolean isHidden() {
\r
230 public long lastModified() {
\r
232 return Simantics.getSession().syncRequest(new Read<Long>() {
\r
234 public Long perform(ReadGraph graph) throws DatabaseException {
\r
235 GraphFileResource gf = GraphFileResource.getInstance(graph);
\r
236 return graph.getRelatedValue(fileResource, gf.LastModified);
\r
239 } catch (DatabaseException e) {
\r
245 public long length() {
\r
247 return getResourceData().length;
\r
248 } catch (DatabaseException e) {
\r
254 public boolean mkdir() {
\r
259 public boolean mkdirs() {
\r
264 public boolean renameTo(File dest) {
\r
269 public boolean setReadable(boolean readable) {
\r
274 public boolean setReadable(boolean readable, boolean ownerOnly) {
\r
279 public boolean setWritable(boolean writable) {
\r
284 public boolean setWritable(boolean writable, boolean ownerOnly) {
\r
289 public boolean setExecutable(boolean executable) {
\r
294 public boolean setExecutable(boolean executable, boolean ownerOnly) {
\r
299 public boolean setLastModified(long time) {
\r
304 public String[] list() {
\r
309 public String[] list(FilenameFilter filter) {
\r
314 public File[] listFiles() {
\r
319 public File[] listFiles(FileFilter filter) {
\r
324 public File[] listFiles(FilenameFilter filter) {
\r
329 public boolean setReadOnly() {
\r
334 public String toString() {
\r
339 public URL toURL() throws MalformedURLException {
\r
344 public URI toURI() {
\r