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