Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.70 KB | None | 0 0
  1. diff --git a/src/main/java/com/github/fge/filesystem/box/attributes/BoxBasicFileAttributesProvider.java b/src/main/java/com/github/fge/filesystem/box/attributes/BoxBasicFileAttributesProvider.java
  2. index f20b2b6..760cc50 100644
  3. --- a/src/main/java/com/github/fge/filesystem/box/attributes/BoxBasicFileAttributesProvider.java
  4. +++ b/src/main/java/com/github/fge/filesystem/box/attributes/BoxBasicFileAttributesProvider.java
  5. @@ -1,46 +1,60 @@
  6. package com.github.fge.filesystem.box.attributes;
  7.  
  8. +import java.io.IOException;
  9. +import java.nio.file.attribute.FileTime;
  10. +import java.nio.file.attribute.GroupPrincipal;
  11. +import java.nio.file.attribute.PosixFileAttributes;
  12. +import java.nio.file.attribute.PosixFilePermission;
  13. +import java.nio.file.attribute.PosixFilePermissions;
  14. +import java.nio.file.attribute.UserPrincipal;
  15. +import java.util.HashMap;
  16. +import java.util.Map;
  17. +import java.util.Set;
  18. +
  19. +import javax.annotation.ParametersAreNonnullByDefault;
  20. +
  21. import com.box.sdk.BoxAPIException;
  22. import com.box.sdk.BoxFolder;
  23. import com.box.sdk.BoxItem;
  24. import com.github.fge.filesystem.attributes.provider.BasicFileAttributesProvider;
  25. -
  26. -
  27. import com.github.fge.filesystem.box.exceptions.BoxIOException;
  28.  
  29. -
  30. -import javax.annotation.ParametersAreNonnullByDefault;
  31. -import java.io.IOException;
  32. -import java.nio.file.attribute.FileTime;
  33. -
  34. @ParametersAreNonnullByDefault
  35. public final class BoxBasicFileAttributesProvider
  36. - extends BasicFileAttributesProvider
  37. + extends BasicFileAttributesProvider implements PosixFileAttributes
  38. {
  39. private final BoxItem.Info info;
  40. private final boolean isFolder;
  41.  
  42. + private Map<String, BoxItem.Info> cache = new HashMap<>();
  43. +
  44. public BoxBasicFileAttributesProvider(final BoxItem item)
  45. throws IOException
  46. {
  47. - try {
  48. - info = item.getInfo();
  49. - } catch (BoxAPIException e) {
  50. - throw BoxIOException.wrap(e);
  51. + String key = item.getID();
  52. + if (cache.containsKey(key)) {
  53. + info = cache.get(key);
  54. + } else {
  55. + try {
  56. + info = item.getInfo();
  57. + } catch (BoxAPIException e) {
  58. + throw BoxIOException.wrap(e);
  59. + }
  60. + cache.put(key, info);
  61. }
  62. isFolder = item instanceof BoxFolder;
  63. }
  64.  
  65. @Override
  66. public FileTime lastModifiedTime()
  67. - {
  68. - return FileTime.fromMillis(info.getModifiedAt().getTime());
  69. - }
  70. + {try {
  71. + return info.getModifiedAt() != null ? FileTime.fromMillis(info.getModifiedAt().getTime()) : creationTime();
  72. + } catch (NullPointerException e) {System.err.println("info: "+ info + ", info.getModifiedAt(): " + info.getModifiedAt());throw e;}}
  73.  
  74. @Override
  75. public FileTime creationTime()
  76. {
  77. - return FileTime.fromMillis(info.getCreatedAt().getTime());
  78. + return info.getCreatedAt() != null ? FileTime.fromMillis(info.getCreatedAt().getTime()) : FileTime.fromMillis(0);
  79. }
  80.  
  81. /**
  82. @@ -75,4 +89,22 @@ public final class BoxBasicFileAttributesProvider
  83. {
  84. return info.getSize();
  85. }
  86. +
  87. + /* @see java.nio.file.attribute.PosixFileAttributes#owner() */
  88. + @Override
  89. + public UserPrincipal owner() {
  90. + return null;
  91. + }
  92. +
  93. + /* @see java.nio.file.attribute.PosixFileAttributes#group() */
  94. + @Override
  95. + public GroupPrincipal group() {
  96. + return null;
  97. + }
  98. +
  99. + /* @see java.nio.file.attribute.PosixFileAttributes#permissions() */
  100. + @Override
  101. + public Set<PosixFilePermission> permissions() {
  102. + return isFolder ? PosixFilePermissions.fromString("rwxr-xr-x") : PosixFilePermissions.fromString("rw-r--r--");
  103. + }
  104. }
  105. diff --git a/src/main/java/com/github/fge/filesystem/box/driver/BoxFileSystemDriver.java b/src/main/java/com/github/fge/filesystem/box/driver/BoxFileSystemDriver.java
  106. index e4261f4..3637ce9 100644
  107. --- a/src/main/java/com/github/fge/filesystem/box/driver/BoxFileSystemDriver.java
  108. +++ b/src/main/java/com/github/fge/filesystem/box/driver/BoxFileSystemDriver.java
  109. @@ -1,21 +1,14 @@
  110. package com.github.fge.filesystem.box.driver;
  111.  
  112. -import com.box.sdk.BoxAPIException;
  113. -import com.box.sdk.BoxFile;
  114. -import com.box.sdk.BoxFolder;
  115. -import com.box.sdk.BoxItem;
  116. -import com.github.fge.filesystem.box.exceptions.BoxIOException;
  117. -import com.github.fge.filesystem.box.io.BoxFileInputStream;
  118. -import com.github.fge.filesystem.box.io.BoxFileOutputStream;
  119. -import com.github.fge.filesystem.driver.UnixLikeFileSystemDriverBase;
  120. -import com.github.fge.filesystem.exceptions.IsDirectoryException;
  121. -import com.github.fge.filesystem.provider.FileSystemFactoryProvider;
  122. -
  123. -import javax.annotation.Nonnull;
  124. -import javax.annotation.ParametersAreNonnullByDefault;
  125. import java.io.IOException;
  126. import java.io.InputStream;
  127. import java.io.OutputStream;
  128. +import java.nio.ByteBuffer;
  129. +import java.nio.channels.Channels;
  130. +import java.nio.channels.NonWritableChannelException;
  131. +import java.nio.channels.ReadableByteChannel;
  132. +import java.nio.channels.SeekableByteChannel;
  133. +import java.nio.channels.WritableByteChannel;
  134. import java.nio.file.AccessDeniedException;
  135. import java.nio.file.AccessMode;
  136. import java.nio.file.CopyOption;
  137. @@ -26,18 +19,35 @@ import java.nio.file.FileStore;
  138. import java.nio.file.NoSuchFileException;
  139. import java.nio.file.OpenOption;
  140. import java.nio.file.Path;
  141. +import java.nio.file.StandardOpenOption;
  142. import java.nio.file.attribute.FileAttribute;
  143. import java.nio.file.spi.FileSystemProvider;
  144. import java.util.ArrayList;
  145. import java.util.Collections;
  146. import java.util.EnumSet;
  147. +import java.util.HashMap;
  148. import java.util.Iterator;
  149. import java.util.List;
  150. +import java.util.Map;
  151. import java.util.Objects;
  152. import java.util.Set;
  153. import java.util.concurrent.ExecutorService;
  154. import java.util.concurrent.Executors;
  155.  
  156. +import javax.annotation.Nonnull;
  157. +import javax.annotation.ParametersAreNonnullByDefault;
  158. +
  159. +import com.box.sdk.BoxAPIException;
  160. +import com.box.sdk.BoxFile;
  161. +import com.box.sdk.BoxFolder;
  162. +import com.box.sdk.BoxItem;
  163. +import com.github.fge.filesystem.box.exceptions.BoxIOException;
  164. +import com.github.fge.filesystem.box.io.BoxFileInputStream;
  165. +import com.github.fge.filesystem.box.io.BoxFileOutputStream;
  166. +import com.github.fge.filesystem.driver.UnixLikeFileSystemDriverBase;
  167. +import com.github.fge.filesystem.exceptions.IsDirectoryException;
  168. +import com.github.fge.filesystem.provider.FileSystemFactoryProvider;
  169. +
  170. /**
  171. * Box filesystem driver
  172. *
  173. @@ -59,10 +69,26 @@ public final class BoxFileSystemDriver
  174. this.wrapper = Objects.requireNonNull(wrapper);
  175. }
  176.  
  177. + /** */
  178. + private Map<String, BoxItem> cache = new HashMap<>();
  179. +
  180. + private BoxItem getItem(final Path path) throws BoxIOException {
  181. + String pathString = path.toAbsolutePath().toString();
  182. + if (cache.containsKey(pathString)) {
  183. + return cache.get(pathString);
  184. + } else {
  185. + BoxItem item = wrapper.getItem(path);
  186. + if (item != null) {
  187. + cache.put(pathString, item);
  188. + }
  189. + return item;
  190. + }
  191. + }
  192. +
  193. @Nonnull
  194. @Override
  195. public InputStream newInputStream(final Path path,
  196. - final Set<OpenOption> options)
  197. + final Set<? extends OpenOption> options)
  198. throws IOException
  199. {
  200. final Path realPath = path.toAbsolutePath();
  201. @@ -76,14 +102,14 @@ public final class BoxFileSystemDriver
  202. @Nonnull
  203. @Override
  204. public OutputStream newOutputStream(final Path path,
  205. - final Set<OpenOption> options)
  206. + final Set<? extends OpenOption> options)
  207. throws IOException
  208. {
  209. final Path realPath = path.toAbsolutePath();
  210.  
  211. final OutputStream ret;
  212. final String target = realPath.toString();
  213. - final BoxItem item = wrapper.getItem(realPath);
  214. + final BoxItem item = getItem(realPath);
  215. final boolean create = item == null;
  216.  
  217. if (create) {
  218. @@ -101,6 +127,8 @@ public final class BoxFileSystemDriver
  219. return ret;
  220. }
  221.  
  222. + private Map<String, List<Path>> folderCache = new HashMap<>();
  223. +
  224. @Nonnull
  225. @Override
  226. public DirectoryStream<Path> newDirectoryStream(final Path dir,
  227. @@ -118,21 +146,30 @@ public final class BoxFileSystemDriver
  228. * to throw that from within an Iterator, we therefore swallow
  229. * everything :/
  230. */
  231. - final List<Path> list = new ArrayList<>();
  232. - try {
  233. - for (final BoxItem.Info info : folder.getChildren("name"))
  234. - list.add(dir.resolve(info.getName()));
  235. - } catch (BoxAPIException e) {
  236. - throw BoxIOException.wrap(e);
  237. + List<Path> list = null;
  238. + if (folderCache.containsKey(realPath.toString())) {
  239. + list = folderCache.get(realPath.toString());
  240. + } else {
  241. + list = new ArrayList<>();
  242. + try {
  243. + for (final BoxItem.Info info : folder.getChildren("name")) {
  244. + list.add(dir.resolve(info.getName()));
  245. + }
  246. + } catch (BoxAPIException e) {
  247. + throw BoxIOException.wrap(e);
  248. + }
  249. + folderCache.put(realPath.toString(), list);
  250. }
  251.  
  252. + final List<Path> list2 = list;
  253. +
  254. //noinspection AnonymousInnerClassWithTooManyMethods
  255. return new DirectoryStream<Path>()
  256. {
  257. @Override
  258. public Iterator<Path> iterator()
  259. {
  260. - return list.iterator();
  261. + return list2.iterator();
  262. }
  263.  
  264. @Override
  265. @@ -144,11 +181,110 @@ public final class BoxFileSystemDriver
  266. }
  267.  
  268. @Override
  269. + public SeekableByteChannel newByteChannel(Path path,
  270. + Set<? extends OpenOption> options,
  271. + FileAttribute<?>... attrs) throws IOException {
  272. + if (options.contains(StandardOpenOption.WRITE) || options.contains(StandardOpenOption.APPEND)) {
  273. + final WritableByteChannel wbc = Channels.newChannel(newOutputStream(path, options));
  274. + long leftover = 0;
  275. + if (options.contains(StandardOpenOption.APPEND)) {
  276. + BoxItem metadata = getItem(path);
  277. + if (metadata != null && asFile(metadata).getInfo().getSize() >= 0)
  278. + leftover = asFile(metadata).getInfo().getSize();
  279. + }
  280. + final long offset = leftover;
  281. + return new SeekableByteChannel() {
  282. + long written = offset;
  283. +
  284. + public boolean isOpen() {
  285. + return wbc.isOpen();
  286. + }
  287. +
  288. + public long position() throws IOException {
  289. + return written;
  290. + }
  291. +
  292. + public SeekableByteChannel position(long pos) throws IOException {
  293. + throw new UnsupportedOperationException();
  294. + }
  295. +
  296. + public int read(ByteBuffer dst) throws IOException {
  297. + throw new UnsupportedOperationException();
  298. + }
  299. +
  300. + public SeekableByteChannel truncate(long size) throws IOException {
  301. + throw new UnsupportedOperationException();
  302. + }
  303. +
  304. + public int write(ByteBuffer src) throws IOException {
  305. + int n = wbc.write(src);
  306. + written += n;
  307. + return n;
  308. + }
  309. +
  310. + public long size() throws IOException {
  311. + return written;
  312. + }
  313. +
  314. + public void close() throws IOException {
  315. + wbc.close();
  316. + }
  317. + };
  318. + } else {
  319. + BoxItem metadata = getItem(path);
  320. + if (isDirectory(metadata))
  321. + throw new NoSuchFileException(path.toString());
  322. + final ReadableByteChannel rbc = Channels.newChannel(newInputStream(path, null));
  323. + final long size = asFile(metadata).getInfo().getSize();
  324. + return new SeekableByteChannel() {
  325. + long read = 0;
  326. +
  327. + public boolean isOpen() {
  328. + return rbc.isOpen();
  329. + }
  330. +
  331. + public long position() throws IOException {
  332. + return read;
  333. + }
  334. +
  335. + public SeekableByteChannel position(long pos) throws IOException {
  336. + read = pos;
  337. + return this;
  338. + }
  339. +
  340. + public int read(ByteBuffer dst) throws IOException {
  341. + int n = rbc.read(dst);
  342. + if (n > 0) {
  343. + read += n;
  344. + }
  345. + return n;
  346. + }
  347. +
  348. + public SeekableByteChannel truncate(long size) throws IOException {
  349. + throw new NonWritableChannelException();
  350. + }
  351. +
  352. + public int write(ByteBuffer src) throws IOException {
  353. + throw new NonWritableChannelException();
  354. + }
  355. +
  356. + public long size() throws IOException {
  357. + return size;
  358. + }
  359. +
  360. + public void close() throws IOException {
  361. + rbc.close();
  362. + }
  363. + };
  364. + }
  365. + }
  366. +
  367. + @Override
  368. public void createDirectory(final Path dir, final FileAttribute<?>... attrs)
  369. throws IOException
  370. {
  371. final Path realPath = dir.toAbsolutePath();
  372. - final BoxItem item = wrapper.getItem(realPath);
  373. + final BoxItem item = getItem(realPath);
  374.  
  375. //noinspection VariableNotUsedInsideIf
  376. if (item != null)
  377. @@ -183,7 +319,7 @@ public final class BoxFileSystemDriver
  378. */
  379. final Path srcPath = source.toAbsolutePath();
  380. final String src = srcPath.toString();
  381. - final BoxItem srcItem = wrapper.getItem(srcPath);
  382. + final BoxItem srcItem = getItem(srcPath);
  383.  
  384. // TODO! metadata driver, yes, again
  385. @SuppressWarnings("ConstantConditions")
  386. @@ -199,7 +335,7 @@ public final class BoxFileSystemDriver
  387. */
  388. final Path dstPath = target.toAbsolutePath();
  389. final String dst = dstPath.toString();
  390. - final BoxItem dstItem = wrapper.getItem(dstPath);
  391. + final BoxItem dstItem = getItem(dstPath);
  392.  
  393. //noinspection VariableNotUsedInsideIf
  394. if (dstItem != null)
  395. @@ -243,7 +379,7 @@ public final class BoxFileSystemDriver
  396. */
  397. final Path srcPath = source.toAbsolutePath();
  398. final String src = srcPath.toString();
  399. - final BoxItem srcItem = wrapper.getItem(srcPath);
  400. + final BoxItem srcItem = getItem(srcPath);
  401.  
  402. // TODO: within a driver, atomic move of non empty directories are OK
  403. @SuppressWarnings("ConstantConditions")
  404. @@ -258,7 +394,7 @@ public final class BoxFileSystemDriver
  405. * replace it, check that it is either a file or a non empty directory.
  406. */
  407. final Path dstPath = target.toAbsolutePath();
  408. - final BoxItem dstItem = wrapper.getItem(dstPath);
  409. + final BoxItem dstItem = getItem(dstPath);
  410.  
  411. //noinspection VariableNotUsedInsideIf
  412. if (dstItem != null)
  413. @@ -286,7 +422,7 @@ public final class BoxFileSystemDriver
  414. {
  415. final Path realPath = path.toAbsolutePath();
  416. final String s = realPath.toString();
  417. - final BoxItem item = wrapper.getItem(realPath);
  418. + final BoxItem item = getItem(realPath);
  419.  
  420. if (item == null)
  421. throw new NoSuchFileException(s);
  422. @@ -306,7 +442,7 @@ public final class BoxFileSystemDriver
  423. {
  424. // TODO: when symlinks are supported this may turn out to be wrong
  425. final Path target = path.toAbsolutePath();
  426. - final BoxItem item = wrapper.getItem(target);
  427. + final BoxItem item = getItem(target);
  428. if (item == null)
  429. throw new NoSuchFileException(target.toString());
  430. return item;
  431. diff --git a/src/main/java/com/github/fge/filesystem/box/opts/BoxFileSystemOptionsFactory.java b/src/main/java/com/github/fge/filesystem/box/opts/BoxFileSystemOptionsFactory.java
  432. index e69de29..f51af8d 100644
  433. --- a/src/main/java/com/github/fge/filesystem/box/opts/BoxFileSystemOptionsFactory.java
  434. +++ b/src/main/java/com/github/fge/filesystem/box/opts/BoxFileSystemOptionsFactory.java
  435. @@ -0,0 +1,27 @@
  436. +/*
  437. + * Copyright (c) 2016 by Naohide Sano, All rights reserved.
  438. + *
  439. + * Programmed by Naohide Sano
  440. + */
  441. +
  442. +package com.github.fge.filesystem.box.opts;
  443. +
  444. +import java.nio.file.LinkOption;
  445. +
  446. +import com.github.fge.filesystem.options.FileSystemOptionsFactory;
  447. +
  448. +
  449. +/**
  450. + * BoxFileSystemOptionsFactory.
  451. + *
  452. + * @author <a href="mailto:umjammer@gmail.com">Naohide Sano</a> (umjammer)
  453. + * @version 0.00 2016/03/11 umjammer initial version <br>
  454. + */
  455. +public class BoxFileSystemOptionsFactory extends FileSystemOptionsFactory {
  456. +
  457. + public BoxFileSystemOptionsFactory() {
  458. + addLinkOption(LinkOption.NOFOLLOW_LINKS);
  459. + }
  460. +}
  461. +
  462. +/* */
  463. diff --git a/src/main/java/com/github/fge/filesystem/box/provider/BoxFileSystemFactoryProvider.java b/src/main/java/com/github/fge/filesystem/box/provider/BoxFileSystemFactoryProvider.java
  464. index 3abb90b..8c9201e 100644
  465. --- a/src/main/java/com/github/fge/filesystem/box/provider/BoxFileSystemFactoryProvider.java
  466. +++ b/src/main/java/com/github/fge/filesystem/box/provider/BoxFileSystemFactoryProvider.java
  467. @@ -1,6 +1,7 @@
  468. package com.github.fge.filesystem.box.provider;
  469.  
  470. import com.github.fge.filesystem.box.attributes.BoxFileAttributesFactory;
  471. +import com.github.fge.filesystem.box.opts.BoxFileSystemOptionsFactory;
  472. import com.github.fge.filesystem.provider.FileSystemFactoryProvider;
  473.  
  474. public final class BoxFileSystemFactoryProvider
  475. @@ -9,5 +10,6 @@ public final class BoxFileSystemFactoryProvider
  476. public BoxFileSystemFactoryProvider()
  477. {
  478. setAttributesFactory(new BoxFileAttributesFactory());
  479. + setOptionsFactory(new BoxFileSystemOptionsFactory());
  480. }
  481. }
  482. diff --git a/src/main/java/com/github/fge/filesystem/box/provider/BoxFileSystemRepository.java b/src/main/java/com/github/fge/filesystem/box/provider/BoxFileSystemRepository.java
  483. index 2981c88..776e864 100644
  484. --- a/src/main/java/com/github/fge/filesystem/box/provider/BoxFileSystemRepository.java
  485. +++ b/src/main/java/com/github/fge/filesystem/box/provider/BoxFileSystemRepository.java
  486. @@ -1,6 +1,15 @@
  487. package com.github.fge.filesystem.box.provider;
  488.  
  489. +import java.io.IOException;
  490. +import java.net.URI;
  491. +import java.nio.file.FileStore;
  492. +import java.util.Map;
  493. +
  494. +import javax.annotation.Nonnull;
  495. +import javax.annotation.ParametersAreNonnullByDefault;
  496. +
  497. import com.box.sdk.BoxAPIConnection;
  498. +import com.box.sdk.BoxAPIConnectionListener;
  499. import com.box.sdk.BoxAPIException;
  500. import com.box.sdk.BoxFolder;
  501. import com.github.fge.filesystem.box.driver.BoxAPIWrapper;
  502. @@ -11,13 +20,6 @@ import com.github.fge.filesystem.box.filestore.BoxFileStore;
  503. import com.github.fge.filesystem.driver.FileSystemDriver;
  504. import com.github.fge.filesystem.provider.FileSystemRepositoryBase;
  505.  
  506. -import javax.annotation.Nonnull;
  507. -import javax.annotation.ParametersAreNonnullByDefault;
  508. -import java.io.IOException;
  509. -import java.net.URI;
  510. -import java.nio.file.FileStore;
  511. -import java.util.Map;
  512. -
  513. @ParametersAreNonnullByDefault
  514. public final class BoxFileSystemRepository
  515. extends FileSystemRepositoryBase
  516. @@ -41,6 +43,16 @@ public final class BoxFileSystemRepository
  517. throw new IllegalArgumentException("access token not found");
  518.  
  519. final BoxAPIConnection api = new BoxAPIConnection(accessToken);
  520. + api.addListener(new BoxAPIConnectionListener() {
  521. + @Override
  522. + public void onRefresh(BoxAPIConnection api) {
  523. + System.out.println("refresh tocken" + api.getRefreshToken());
  524. + }
  525. + @Override
  526. + public void onError(BoxAPIConnection api, BoxAPIException error) {
  527. + error.printStackTrace();
  528. + }
  529. + });
  530. final BoxAPIWrapper wrapper = new DefaultBoxAPIWrapper(api);
  531. final FileStore store;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement