SimpleDirectoryPersistentCache.java

  1. /**
  2.  *
  3.  * Copyright © 2011-2019 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.caps.cache;

  18. import java.io.DataInputStream;
  19. import java.io.DataOutputStream;
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;

  26. import org.jivesoftware.smack.util.PacketParserUtils;
  27. import org.jivesoftware.smack.util.stringencoder.Base32;
  28. import org.jivesoftware.smack.util.stringencoder.StringEncoder;

  29. import org.jivesoftware.smackx.disco.packet.DiscoverInfo;

  30. /**
  31.  * Simple implementation of an EntityCapsPersistentCache that uses a directory
  32.  * to store the Caps information for every known node. Every node is represented
  33.  * by a file.
  34.  *
  35.  * @author Florian Schmaus
  36.  *
  37.  */
  38. public class SimpleDirectoryPersistentCache implements EntityCapsPersistentCache {
  39.     private static final Logger LOGGER = Logger.getLogger(SimpleDirectoryPersistentCache.class.getName());

  40.     private final File cacheDir;
  41.     private final StringEncoder<String> filenameEncoder;

  42.     /**
  43.      * Creates a new SimpleDirectoryPersistentCache Object. Make sure that the
  44.      * cacheDir exists and that it's an directory.
  45.      * <p>
  46.      * Default filename encoder {@link Base32}, as this will work on all
  47.      * file systems, both case sensitive and case insensitive.  It does however
  48.      * produce longer filenames.
  49.      *
  50.      * @param cacheDir TODO javadoc me please
  51.      */
  52.     public SimpleDirectoryPersistentCache(File cacheDir) {
  53.         this(cacheDir, Base32.getStringEncoder());
  54.     }

  55.     /**
  56.      * Creates a new SimpleDirectoryPersistentCache Object. Make sure that the
  57.      * cacheDir exists and that it's an directory.
  58.      *
  59.      * If your cacheDir is case insensitive then make sure to set the
  60.      * StringEncoder to {@link Base32} (which is the default).
  61.      *
  62.      * @param cacheDir The directory where the cache will be stored.
  63.      * @param filenameEncoder Encodes the node string into a filename.
  64.      */
  65.     public SimpleDirectoryPersistentCache(File cacheDir, StringEncoder<String> filenameEncoder) {
  66.         if (!cacheDir.exists())
  67.             throw new IllegalStateException("Cache directory \"" + cacheDir + "\" does not exist");
  68.         if (!cacheDir.isDirectory())
  69.             throw new IllegalStateException("Cache directory \"" + cacheDir + "\" is not a directory");

  70.         this.cacheDir = cacheDir;
  71.         this.filenameEncoder = filenameEncoder;
  72.     }

  73.     @Override
  74.     public void addDiscoverInfoByNodePersistent(String nodeVer, DiscoverInfo info) {
  75.         File nodeFile = getFileFor(nodeVer);
  76.         try {
  77.             if (nodeFile.createNewFile())
  78.                 writeInfoToFile(nodeFile, info);
  79.         } catch (IOException e) {
  80.             LOGGER.log(Level.SEVERE, "Failed to write disco info to file", e);
  81.         }
  82.     }

  83.     @Override
  84.     public DiscoverInfo lookup(String nodeVer) {
  85.         File nodeFile = getFileFor(nodeVer);
  86.         if (!nodeFile.isFile()) {
  87.             return null;
  88.         }
  89.         DiscoverInfo info = null;
  90.         try {
  91.             info = restoreInfoFromFile(nodeFile);
  92.         }
  93.         catch (Exception e) {
  94.             LOGGER.log(Level.WARNING, "Coud not restore info from file", e);
  95.         }
  96.         return info;
  97.     }

  98.     private File getFileFor(String nodeVer) {
  99.         String filename = filenameEncoder.encode(nodeVer);
  100.         return new File(cacheDir, filename);
  101.     }

  102.     @Override
  103.     public void emptyCache() {
  104.         File[] files = cacheDir.listFiles();
  105.         if (files == null) {
  106.             return;
  107.         }
  108.         for (File f : files) {
  109.             f.delete();
  110.         }
  111.     }

  112.     /**
  113.      * Writes the DiscoverInfo stanza to an file
  114.      *
  115.      * @param file TODO javadoc me please
  116.      * @param info TODO javadoc me please
  117.      * @throws IOException if an I/O error occurred.
  118.      */
  119.     private static void writeInfoToFile(File file, DiscoverInfo info) throws IOException {
  120.         try (DataOutputStream dos = new DataOutputStream(new FileOutputStream(file))) {
  121.             dos.writeUTF(info.toXML().toString());
  122.         }
  123.     }

  124.     /**
  125.      * Tries to restore an DiscoverInfo stanza from a file.
  126.      *
  127.      * @param file TODO javadoc me please
  128.      * @return the restored DiscoverInfo
  129.      * @throws Exception if an exception occurs.
  130.      */
  131.     private static DiscoverInfo restoreInfoFromFile(File file) throws Exception {
  132.         String fileContent;
  133.         try (DataInputStream dis = new DataInputStream(new FileInputStream(file))) {
  134.             fileContent = dis.readUTF();
  135.         }
  136.         if (fileContent == null) {
  137.             return null;
  138.         }
  139.         return PacketParserUtils.parseStanza(fileContent);
  140.     }
  141. }