Skip to content

Commit

Permalink
Refactor standard persistence provider to use hierarchical directorie…
Browse files Browse the repository at this point in the history
…s and include properties file
  • Loading branch information
bbende committed Jun 6, 2024
1 parent 6987981 commit 9437895
Show file tree
Hide file tree
Showing 8 changed files with 640 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.nifi.nar;

import java.io.File;
import java.util.Objects;

/**
* The information about a NAR persisted by a {@link NarPersistenceProvider}.
*/
public class NarPersistenceInfo {

private final File narFile;
private final NarProperties narProperties;

public NarPersistenceInfo(final File narFile, final NarProperties narProperties) {
this.narFile = Objects.requireNonNull(narFile);
this.narProperties = Objects.requireNonNull(narProperties);
}

public File getNarFile() {
return narFile;
}

public NarProperties getNarProperties() {
return narProperties;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final NarPersistenceInfo that = (NarPersistenceInfo) o;
return Objects.equals(narProperties.getCoordinate(), that.narProperties.getCoordinate());
}

@Override
public int hashCode() {
return Objects.hash(narProperties.getCoordinate());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Set;

/**
* Provides persistence for NAR files dynamically added to NiFi.
Expand All @@ -35,7 +35,7 @@ public interface NarPersistenceProvider {
*
* @param initializationContext the context containing and properties and configuration required to initialize the provider
*/
void initialize(NarPersistenceProviderInitializationContext initializationContext);
void initialize(NarPersistenceProviderInitializationContext initializationContext) throws IOException;

/**
* Creates a temporary file containing the contents of the given input stream.
Expand All @@ -53,11 +53,11 @@ public interface NarPersistenceProvider {
*
* @param persistenceContext the context information
* @param tempNarFile the temporary file created from calling createTempFile
* @return the saved file
* @return the information about the persisted NAR, including the saved file
*
* @throws IOException if an I/O error occurs saving the temp NAR file
*/
File saveNar(NarPersistenceContext persistenceContext, File tempNarFile) throws IOException;
NarPersistenceInfo saveNar(NarPersistenceContext persistenceContext, File tempNarFile) throws IOException;

/**
* Deletes the contents of the NAR with the given coordinate.
Expand All @@ -79,6 +79,15 @@ public interface NarPersistenceProvider {
*/
InputStream readNar(BundleCoordinate narCoordinate) throws FileNotFoundException;

/**
* Retrieves the persistence info for the NAR with the given NAR coordinate.
*
* @param narCoordinate the coordinate of the NAR
* @return the persistence info
* @throws IOException if an I/O error occurs reading the persistence info
*/
NarPersistenceInfo getNarInfo(BundleCoordinate narCoordinate) throws IOException;

/**
* Indicates if a NAR with the given coordinate exists in this persistence provider.
*
Expand All @@ -88,9 +97,9 @@ public interface NarPersistenceProvider {
boolean exists(BundleCoordinate narCoordinate);

/**
* @return a map from NAR coordinate to NAR File for all NARs saved to this persistence provider
* @return the info for all NARs persisted by this persistence provider
*/
Map<BundleCoordinate, File> getNarFiles();
Set<NarPersistenceInfo> getAllNarInfo() throws IOException;

/**
* Called prior to NiFi shutting down.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
/*
* 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.nifi.nar;

import org.apache.nifi.bundle.BundleCoordinate;

import java.io.IOException;
import java.io.InputStream;
import java.time.Instant;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;

/**
* Properties about a NAR that are persisted by the {@link NarPersistenceProvider}.
*/
public class NarProperties {

private final String sourceType;
private final String sourceId;

private final String narGroup;
private final String narId;
private final String narVersion;

private final String narDependencyGroup;
private final String narDependencyId;
private final String narDependencyVersion;

private final Instant created;

private NarProperties(final Builder builder) {
this.sourceType = Objects.requireNonNull(builder.sourceType);
this.sourceId = builder.sourceId;

this.narGroup = Objects.requireNonNull(builder.narGroup);
this.narId = Objects.requireNonNull(builder.narId);
this.narVersion = Objects.requireNonNull(builder.narVersion);

this.narDependencyGroup = builder.narDependencyGroup;
this.narDependencyId = builder.narDependencyId;
this.narDependencyVersion = builder.narDependencyVersion;

this.created = Objects.requireNonNull(builder.created);
}

public String getSourceType() {
return sourceType;
}

public String getSourceId() {
return sourceId;
}

public String getNarGroup() {
return narGroup;
}

public String getNarId() {
return narId;
}

public String getNarVersion() {
return narVersion;
}

public String getNarDependencyGroup() {
return narDependencyGroup;
}

public String getNarDependencyId() {
return narDependencyId;
}

public String getNarDependencyVersion() {
return narDependencyVersion;
}

public Instant getCreated() {
return created;
}

public BundleCoordinate getCoordinate() {
return new BundleCoordinate(narGroup, narId, narVersion);
}

public Optional<BundleCoordinate> getDependencyCoordinate() {
if (narDependencyGroup == null) {
return Optional.empty();
}
return Optional.of(new BundleCoordinate(narDependencyGroup, narDependencyId, narDependencyVersion));
}

public Properties toProperties() {
final Properties properties = new Properties();
properties.put(NarProperty.SOURCE_TYPE.getKey(), sourceType);
if (sourceId != null) {
properties.put(NarProperty.SOURCE_ID.getKey(), sourceId);
}

properties.put(NarProperty.NAR_GROUP.getKey(), narGroup);
properties.put(NarProperty.NAR_ID.getKey(), narId);
properties.put(NarProperty.NAR_VERSION.getKey(), narVersion);

if (narDependencyGroup != null) {
properties.put(NarProperty.NAR_DEPENDENCY_GROUP.getKey(), narDependencyGroup);
properties.put(NarProperty.NAR_DEPENDENCY_ID.getKey(), narDependencyId);
properties.put(NarProperty.NAR_DEPENDENCY_VERSION.getKey(), narDependencyVersion);
}

properties.put(NarProperty.CREATED.getKey(), created.toString());
return properties;
}

public static NarProperties parse(final InputStream inputStream) throws IOException {
final Properties properties = new Properties();
properties.load(inputStream);

final String createdValue = properties.getProperty(NarProperty.CREATED.getKey());
final Instant created = Instant.parse(createdValue);

return NarProperties.builder()
.sourceType(properties.getProperty(NarProperty.SOURCE_TYPE.getKey()))
.sourceId(properties.getProperty(NarProperty.SOURCE_ID.getKey()))
.narGroup(properties.getProperty(NarProperty.NAR_GROUP.getKey()))
.narId(properties.getProperty(NarProperty.NAR_ID.getKey()))
.narVersion(properties.getProperty(NarProperty.NAR_VERSION.getKey()))
.narDependencyGroup(properties.getProperty(NarProperty.NAR_DEPENDENCY_GROUP.getKey()))
.narDependencyId(properties.getProperty(NarProperty.NAR_DEPENDENCY_ID.getKey()))
.narDependencyVersion(properties.getProperty(NarProperty.NAR_DEPENDENCY_VERSION.getKey()))
.created(created)
.build();
}

public static Builder builder() {
return new Builder();
}

public static final class Builder {
private String sourceType;
private String sourceId;
private String narGroup;
private String narId;
private String narVersion;
private String narDependencyGroup;
private String narDependencyId;
private String narDependencyVersion;
private Instant created;

public Builder sourceType(final String sourceType) {
this.sourceType = sourceType;
return this;
}

public Builder sourceId(final String sourceId) {
this.sourceId = sourceId;
return this;
}

public Builder narGroup(final String narGroup) {
this.narGroup = narGroup;
return this;
}

public Builder narId(final String narId) {
this.narId = narId;
return this;
}

public Builder narVersion(final String narVersion) {
this.narVersion = narVersion;
return this;
}

public Builder narDependencyGroup(final String narDependencyGroup) {
this.narDependencyGroup = narDependencyGroup;
return this;
}

public Builder narDependencyId(final String narDependencyId) {
this.narDependencyId = narDependencyId;
return this;
}

public Builder narDependencyVersion(final String narDependencyVersion) {
this.narDependencyVersion = narDependencyVersion;
return this;
}

public Builder created(final Instant created) {
this.created = created;
return this;
}

public NarProperties build() {
return new NarProperties(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.nifi.nar;

/**
* Keys for {@link NarProperties}.
*/
public enum NarProperty {

SOURCE_TYPE("source.type"),
SOURCE_ID("source.id"),
NAR_GROUP("nar.group"),
NAR_ID("nar.id"),
NAR_VERSION("nar.version"),
NAR_DEPENDENCY_GROUP("nar.dependency.group"),
NAR_DEPENDENCY_ID("nar.dependency.id"),
NAR_DEPENDENCY_VERSION("nar.dependency.version"),
CREATED("created");

private final String key;

NarProperty(final String key) {
this.key = key;
}

public String getKey() {
return key;
}
}
Loading

0 comments on commit 9437895

Please sign in to comment.