Author: olli Date: Fri Oct 23 10:18:22 2015 New Revision: 1710166 URL: http://svn.apache.org/viewvc?rev=1710166&view=rev Log: SLING-5190 factor out of SlingSettingsServiceImpl a Sling ID util
* move readSlingId(File, int):String and writeSlingId(File, String):void from SlingSettingsServiceImpl to new class SlingIdUtil Added: sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SlingIdUtil.java Modified: sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java sling/trunk/bundles/extensions/settings/src/test/java/org/apache/sling/settings/impl/SlingSettingsServiceImplTest.java Added: sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SlingIdUtil.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SlingIdUtil.java?rev=1710166&view=auto ============================================================================== --- sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SlingIdUtil.java (added) +++ sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SlingIdUtil.java Fri Oct 23 10:18:22 2015 @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.sling.settings.impl; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.UUID; + +public class SlingIdUtil { + + /** + * Read the id from a file. + */ + static String readSlingId(final File idFile, int maxLength) { + if (idFile.exists() && idFile.length() >= maxLength) { + DataInputStream dis = null; + try { + final byte[] rawBytes = new byte[maxLength]; + dis = new DataInputStream(new FileInputStream(idFile)); + dis.readFully(rawBytes); + final String rawString = new String(rawBytes, "ISO-8859-1"); + + // roundtrip to ensure correct format of UUID value + final String id = UUID.fromString(rawString).toString(); + // logger.debug("Got Sling ID {} from file {}", id, idFile); + + return id; + } catch (final Throwable t) { + // logger.error("Failed reading UUID from id file " + idFile + // + ", creating new id", t); + } finally { + if (dis != null) { + try { + dis.close(); + } catch (IOException ignore){} + } + } + } + return null; + } + + /** + * Write the sling id file. + */ + static void writeSlingId(final File idFile, final String id) { + idFile.delete(); + idFile.getParentFile().mkdirs(); + DataOutputStream dos = null; + try { + final byte[] rawBytes = id.getBytes("ISO-8859-1"); + dos = new DataOutputStream(new FileOutputStream(idFile)); + dos.write(rawBytes, 0, rawBytes.length); + dos.flush(); + } catch (final Throwable t) { + // logger.error("Failed writing UUID to id file " + idFile, t); + } finally { + if (dos != null) { + try { + dos.close(); + } catch (IOException ignore) {} + } + } + } + +} Modified: sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java?rev=1710166&r1=1710165&r2=1710166&view=diff ============================================================================== --- sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java (original) +++ sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java Fri Oct 23 10:18:22 2015 @@ -18,8 +18,6 @@ */ package org.apache.sling.settings.impl; -import java.io.DataInputStream; -import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -129,12 +127,12 @@ public class SlingSettingsServiceImpl // the osgi framework does not support storing something in the file system throw new RuntimeException("Unable to read from bundle data file."); } - this.slingId = this.readSlingId(idFile, SLING_ID_LENGTH); + this.slingId = SlingIdUtil.readSlingId(idFile, SLING_ID_LENGTH); // no sling id yet or failure to read file: create an id and store if (slingId == null) { slingId = UUID.randomUUID().toString(); - this.writeSlingId(idFile, this.slingId); + SlingIdUtil.writeSlingId(idFile, this.slingId); } } @@ -288,60 +286,6 @@ public class SlingSettingsServiceImpl } } } - - /** - * Read the id from a file. - */ - String readSlingId(final File idFile, int maxLength) { - if (idFile.exists() && idFile.length() >= maxLength) { - DataInputStream dis = null; - try { - final byte[] rawBytes = new byte[maxLength]; - dis = new DataInputStream(new FileInputStream(idFile)); - dis.readFully(rawBytes); - final String rawString = new String(rawBytes, "ISO-8859-1"); - - // roundtrip to ensure correct format of UUID value - final String id = UUID.fromString(rawString).toString(); - logger.debug("Got Sling ID {} from file {}", id, idFile); - - return id; - } catch (final Throwable t) { - logger.error("Failed reading UUID from id file " + idFile - + ", creating new id", t); - } finally { - if (dis != null) { - try { - dis.close(); - } catch (IOException ignore){} - } - } - } - return null; - } - - /** - * Write the sling id file. - */ - void writeSlingId(final File idFile, final String id) { - idFile.delete(); - idFile.getParentFile().mkdirs(); - DataOutputStream dos = null; - try { - final byte[] rawBytes = id.getBytes("ISO-8859-1"); - dos = new DataOutputStream(new FileOutputStream(idFile)); - dos.write(rawBytes, 0, rawBytes.length); - dos.flush(); - } catch (final Throwable t) { - logger.error("Failed writing UUID to id file " + idFile, t); - } finally { - if (dos != null) { - try { - dos.close(); - } catch (IOException ignore) {} - } - } - } /** * @see org.apache.sling.settings.SlingSettingsService#getAbsolutePathWithinSlingHome(String) Modified: sling/trunk/bundles/extensions/settings/src/test/java/org/apache/sling/settings/impl/SlingSettingsServiceImplTest.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/settings/src/test/java/org/apache/sling/settings/impl/SlingSettingsServiceImplTest.java?rev=1710166&r1=1710165&r2=1710166&view=diff ============================================================================== --- sling/trunk/bundles/extensions/settings/src/test/java/org/apache/sling/settings/impl/SlingSettingsServiceImplTest.java (original) +++ sling/trunk/bundles/extensions/settings/src/test/java/org/apache/sling/settings/impl/SlingSettingsServiceImplTest.java Fri Oct 23 10:18:22 2015 @@ -108,13 +108,13 @@ public class SlingSettingsServiceImplTes private String readSlingId(File slingIdFile, File optionsFile, int maxLength) throws IOException { SlingSettingsServiceImpl settings = getSlingSettings(slingIdFile, optionsFile); - return settings.readSlingId(slingIdFile, maxLength); + return SlingIdUtil.readSlingId(slingIdFile, maxLength); } private void writeSlingId(File slingIdFile, File optionsFile, String slingId) throws IOException { SlingSettingsServiceImpl settings = getSlingSettings(slingIdFile, optionsFile); - settings.writeSlingId(slingIdFile, slingId); + SlingIdUtil.writeSlingId(slingIdFile, slingId); } private SlingSettingsServiceImpl getSlingSettings(File slingIdFile, File optionsFile)