Skip to content

Commit

Permalink
correcting the loading of the vdb when supplied in multiple .ddl or .…
Browse files Browse the repository at this point in the history
…sql files (#318)
  • Loading branch information
rareddy committed Jun 22, 2021
1 parent 53a5176 commit 7c4e118
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -27,6 +28,8 @@

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = { Application.class, TestConfiguration.class})
//turned off due to dependency on external API for Employee service but one can run this fine
@Ignore
public class TestExample {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;
import java.sql.Driver;
Expand Down Expand Up @@ -61,7 +62,9 @@
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.jdbc.datasource.embedded.ConnectionProperties;
import org.springframework.jdbc.datasource.embedded.DataSourceFactory;
Expand Down Expand Up @@ -182,7 +185,7 @@ public VDBMetaData teiidVDB() {
DeploymentBasedDatabaseStore store = new DeploymentBasedDatabaseStore(new VDBRepository());
String db = ObjectConverterUtil.convertToString(resources.get(0).getInputStream());
vdb = store.getVDBMetadata(db);
VDBResources vdbResources = buildVdbResources(getParent(resource.getURI().toString()));
VDBResources vdbResources = buildVdbResources(getParent(resource));
vdb.addAttachment(VDBResources.class, vdbResources);
logger.info("Predefined VDB found = " + resource.getFilename());
} catch (IOException e) {
Expand All @@ -191,7 +194,7 @@ public VDBMetaData teiidVDB() {
} else if (resource.getFilename().endsWith("-vdb.xml")) {
try {
vdb = VDBMetadataParser.unmarshall(resource.getInputStream());
VDBResources vdbResources = buildVdbResources(getParent(resource.getURI().toString()));
VDBResources vdbResources = buildVdbResources(getParent(resource));
vdb.addAttachment(VDBResources.class, vdbResources);
logger.info("Predefined VDB found = " + resource.getFilename());
} catch (XMLStreamException | IOException e) {
Expand All @@ -217,7 +220,16 @@ public VDBMetaData teiidVDB() {
return vdb;
}

private String getParent(String resourcePath) {
private String getParent(Resource resource) throws IOException {
String resourcePath = resource.getURI().toString();
if (resource instanceof FileSystemResource) {
resourcePath = ((FileSystemResource)resource).getFile().getPath().toString();
}
int indexColon = resourcePath.lastIndexOf(':');
if (indexColon != -1) {
// this is a url based resource
resourcePath = resourcePath.substring(indexColon+1);
}
int index = resourcePath.lastIndexOf('/');
return index > 0 ? resourcePath.substring(0, index + 1) : "/";
}
Expand All @@ -227,13 +239,22 @@ private VDBResources buildVdbResources(String curdir) throws IOException {
"*/*.sql");
LinkedHashMap<String, VDBResources.Resource> files = new LinkedHashMap<>();
for (Resource r : resources) {
final String resourcePath = r.getURI().toString();
// remove shared parents
final String shortenedPath = Stream.of(resourcePath.split("/"))
.reduce((s1, s2) -> curdir.contains(s1 + "/" + s2) ? s1 + "/" + s2 : s1)
.map(s -> resourcePath.replace(s + "/", ""))
.orElse(resourcePath);
files.put(shortenedPath, new VDBResources.Resource(new NioVirtualFile(Paths.get(shortenedPath))));
if (r instanceof FileSystemResource) {
Path p = r.getFile().toPath();
String path = p.toString().replace(curdir, "");
if (path.startsWith("/")) {
path = path.substring(1);
}
files.put(path, new VDBResources.Resource(new NioVirtualFile(p)));
} else if (r instanceof UrlResource) {
String resourcePath = r.getURI().toString();
int indexColon = resourcePath.lastIndexOf(':');
if (indexColon != -1) {
resourcePath = resourcePath.substring(indexColon+1);
}
String shortenedPath = resourcePath.replace(curdir, "");
files.put(shortenedPath, new VDBResources.Resource(new UrlResourceVirtualFile(shortenedPath, (UrlResource)r)));
}
}
VDBResources vdbResources = new VDBResources(new NioVirtualFile(Paths.get("application.properties")));
vdbResources.getEntriesPlusVisibilities().putAll(files);
Expand Down Expand Up @@ -455,4 +476,4 @@ public TrustManager trustManager() throws IOException {
public ExternalSources externalSources() {
return new ExternalSources();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed 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.teiid.spring.autoconfigure;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.springframework.core.io.UrlResource;
import org.teiid.query.metadata.VirtualFile;


public class UrlResourceVirtualFile implements VirtualFile {
private UrlResource resource;
String name;

public UrlResourceVirtualFile(String name, UrlResource resource) {
this.name = name;
this.resource = resource;
}

@Override
public InputStream openStream() throws IOException {
return resource.getInputStream();
}

@Override
public long getSize() {
return 0;
}

@Override
public String getName() {
return name;
}

@Override
public List<VirtualFile> getFileChildrenRecursively() throws IOException {
return null;
}

@Override
public boolean isFile() {
return false;
}

@Override
public String getPathName() {
return this.name;
}

@Override
public VirtualFile getChild(String string) {
return null;
}

@Override
public boolean exists() {
return true;
}
}

0 comments on commit 7c4e118

Please sign in to comment.