If the lock is available, grant it + * to the calling thread and block all operations from other threads. + * If not available, wait for the holder of the lock to release it with + * {@link #unlock()} before any other operations can be performed. + * + *
Locking behavior is reentrant, which means a thread can invoke + * multiple times, where a hold count is maintained. + */ + void lock(); + + /** + * Unlock storage. + * + *
Locking behavior is reentrant, thus if the thread invoked + * {@link #lock()} multiple times, the hold count is decremented. If the + * hold count reaches 0, the lock is released. + * + *
If the current thread does not hold the lock, an
+ * {@link IllegalMonitorStateException} will be thrown.
+ */
+ void unlock();
+}
diff --git a/src/main/java/io/github/eocqrs/xfake/ImmutableReentrantLock.java b/src/main/java/io/github/eocqrs/xfake/ImmutableReentrantLock.java
new file mode 100644
index 0000000..7fb8baa
--- /dev/null
+++ b/src/main/java/io/github/eocqrs/xfake/ImmutableReentrantLock.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2023 Aliaksei Bialiauski, EO-CQRS
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package io.github.eocqrs.xfake;
+
+import java.io.Serial;
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * Immutable Lock.
+ *
+ * @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
+ * @since 0.0.0
+ */
+final class ImmutableReentrantLock extends ReentrantLock {
+
+ /**
+ * Serialization id.
+ */
+ @Serial
+ private static final long serialVersionUID = -3003135962114984635L;
+}
diff --git a/src/main/java/io/github/eocqrs/xfake/InFile.java b/src/main/java/io/github/eocqrs/xfake/InFile.java
new file mode 100644
index 0000000..1676d8a
--- /dev/null
+++ b/src/main/java/io/github/eocqrs/xfake/InFile.java
@@ -0,0 +1,125 @@
+/*
+ * Copyright (c) 2023 Aliaksei Bialiauski, EO-CQRS
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package io.github.eocqrs.xfake;
+
+import com.jcabi.xml.XML;
+import com.jcabi.xml.XMLDocument;
+import org.cactoos.io.TeeInput;
+import org.cactoos.scalar.LengthOf;
+import org.cactoos.text.TextOf;
+import org.xembly.Directive;
+import org.xembly.Xembler;
+
+import java.io.File;
+import java.nio.charset.StandardCharsets;
+
+/**
+ * Storage in file.
+ *
+ * @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
+ * @since 0.0.0
+ */
+public final class InFile implements FkStorage {
+
+ /**
+ * File name.
+ */
+ private final transient String name;
+ /**
+ * Lock.
+ */
+ private final transient ImmutableReentrantLock lock =
+ new ImmutableReentrantLock();
+
+ /**
+ * Ctor.
+ *
+ * @param nm File name to create
+ * @param root root XML node
+ * @throws Exception When something went wrong.
+ */
+ public InFile(final String nm, final String root) throws Exception {
+ this(File.createTempFile(nm, ".xml"), root);
+ new File(this.name).deleteOnExit();
+ }
+
+ /**
+ * Ctor.
+ *
+ * @param file File
+ * @param root root XML node
+ * @throws Exception When something went wrong.
+ */
+ public InFile(final File file, final String root) throws Exception {
+ new LengthOf(
+ new TeeInput(
+ root,
+ file,
+ StandardCharsets.UTF_8
+ )
+ ).value();
+ this.name = file.getAbsolutePath();
+ }
+
+ @Override
+ public XML xml() throws Exception {
+ synchronized (this.name) {
+ return new XMLDocument(
+ new TextOf(
+ new File(
+ this.name
+ )
+ ).asString()
+ );
+ }
+ }
+
+ @Override
+ public void apply(final Iterable