summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/db
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/db')
-rw-r--r--src/main/java/org/openslx/imagemaster/db/MysqlConnection.java8
-rw-r--r--src/main/java/org/openslx/imagemaster/db/MysqlStatement.java190
-rw-r--r--src/main/java/org/openslx/imagemaster/db/mappers/DbImage.java223
-rw-r--r--src/main/java/org/openslx/imagemaster/db/mappers/DbOperatingSystem.java36
-rw-r--r--src/main/java/org/openslx/imagemaster/db/mappers/DbOsVirt.java108
-rw-r--r--src/main/java/org/openslx/imagemaster/db/mappers/DbPendingSatellite.java49
-rw-r--r--src/main/java/org/openslx/imagemaster/db/mappers/DbSatellite.java62
-rw-r--r--src/main/java/org/openslx/imagemaster/db/mappers/DbUser.java18
-rw-r--r--src/main/java/org/openslx/imagemaster/db/models/LocalSatellite.java22
9 files changed, 379 insertions, 337 deletions
diff --git a/src/main/java/org/openslx/imagemaster/db/MysqlConnection.java b/src/main/java/org/openslx/imagemaster/db/MysqlConnection.java
index dcfa713..10c263b 100644
--- a/src/main/java/org/openslx/imagemaster/db/MysqlConnection.java
+++ b/src/main/java/org/openslx/imagemaster/db/MysqlConnection.java
@@ -24,11 +24,15 @@ public class MysqlConnection implements AutoCloseable {
MysqlConnection(Connection rawConnection) {
this.rawConnection = rawConnection;
}
-
+
public MysqlStatement prepareStatement(String sql) throws SQLException {
+ return prepareStatement( sql, false );
+ }
+
+ public MysqlStatement prepareStatement(String sql, boolean getKeys) throws SQLException {
if (!sql.startsWith("SELECT"))
hasPendingQueries = true;
- MysqlStatement statement = new MysqlStatement(rawConnection, sql);
+ MysqlStatement statement = new MysqlStatement(rawConnection, sql, getKeys);
openStatements.add(statement);
return statement;
}
diff --git a/src/main/java/org/openslx/imagemaster/db/MysqlStatement.java b/src/main/java/org/openslx/imagemaster/db/MysqlStatement.java
index 391aed0..3dda36a 100644
--- a/src/main/java/org/openslx/imagemaster/db/MysqlStatement.java
+++ b/src/main/java/org/openslx/imagemaster/db/MysqlStatement.java
@@ -18,7 +18,8 @@ import java.util.Map;
* "http://www.javaworld.com/article/2077706/core-java/named-parameters-for-preparedstatement.html?page=2"
* >Named Parameters for PreparedStatement</a>
*/
-public class MysqlStatement implements Closeable {
+public class MysqlStatement implements Closeable
+{
private static final QueryCache cache = new QueryCache();
@@ -28,19 +29,24 @@ public class MysqlStatement implements Closeable {
private final List<ResultSet> openResultSets = new ArrayList<>();
- MysqlStatement(Connection con, String sql) throws SQLException {
+ MysqlStatement( Connection con, String sql, boolean getKeys ) throws SQLException
+ {
PreparsedQuery query;
- synchronized (cache) {
- query = cache.get(sql);
+ synchronized ( cache ) {
+ query = cache.get( sql );
}
- if (query == null) {
- query = parse(sql);
- synchronized (cache) {
- cache.put(sql, query);
+ if ( query == null ) {
+ query = parse( sql );
+ synchronized ( cache ) {
+ cache.put( sql, query );
}
}
this.query = query;
- this.statement = con.prepareStatement(query.sql);
+ if ( getKeys ) {
+ this.statement = con.prepareStatement( query.sql, Statement.RETURN_GENERATED_KEYS );
+ } else {
+ this.statement = con.prepareStatement( query.sql );
+ }
}
/**
@@ -50,10 +56,11 @@ public class MysqlStatement implements Closeable {
* @return parameter indexes
* @throws IllegalArgumentException if the parameter does not exist
*/
- private List<Integer> getIndexes(String name) {
- List<Integer> indexes = query.indexMap.get(name);
- if (indexes == null) {
- throw new IllegalArgumentException("Parameter not found: " + name);
+ private List<Integer> getIndexes( String name )
+ {
+ List<Integer> indexes = query.indexMap.get( name );
+ if ( indexes == null ) {
+ throw new IllegalArgumentException( "Parameter not found: " + name );
}
return indexes;
}
@@ -67,10 +74,11 @@ public class MysqlStatement implements Closeable {
* @throws IllegalArgumentException if the parameter does not exist
* @see PreparedStatement#setObject(int, java.lang.Object)
*/
- public void setObject(String name, Object value) throws SQLException {
- List<Integer> indexes = getIndexes(name);
- for (Integer index : indexes) {
- statement.setObject(index, value);
+ public void setObject( String name, Object value ) throws SQLException
+ {
+ List<Integer> indexes = getIndexes( name );
+ for ( Integer index : indexes ) {
+ statement.setObject( index, value );
}
}
@@ -83,10 +91,11 @@ public class MysqlStatement implements Closeable {
* @throws IllegalArgumentException if the parameter does not exist
* @see PreparedStatement#setString(int, java.lang.String)
*/
- public void setString(String name, String value) throws SQLException {
- List<Integer> indexes = getIndexes(name);
- for (Integer index : indexes) {
- statement.setString(index, value);
+ public void setString( String name, String value ) throws SQLException
+ {
+ List<Integer> indexes = getIndexes( name );
+ for ( Integer index : indexes ) {
+ statement.setString( index, value );
}
}
@@ -99,10 +108,11 @@ public class MysqlStatement implements Closeable {
* @throws IllegalArgumentException if the parameter does not exist
* @see PreparedStatement#setInt(int, int)
*/
- public void setInt(String name, int value) throws SQLException {
- List<Integer> indexes = getIndexes(name);
- for (Integer index : indexes) {
- statement.setInt(index, value);
+ public void setInt( String name, int value ) throws SQLException
+ {
+ List<Integer> indexes = getIndexes( name );
+ for ( Integer index : indexes ) {
+ statement.setInt( index, value );
}
}
@@ -115,10 +125,11 @@ public class MysqlStatement implements Closeable {
* @throws IllegalArgumentException if the parameter does not exist
* @see PreparedStatement#setLong(int, long)
*/
- public void setLong(String name, long value) throws SQLException {
- List<Integer> indexes = getIndexes(name);
- for (Integer index : indexes) {
- statement.setLong(index, value);
+ public void setLong( String name, long value ) throws SQLException
+ {
+ List<Integer> indexes = getIndexes( name );
+ for ( Integer index : indexes ) {
+ statement.setLong( index, value );
}
}
@@ -131,10 +142,11 @@ public class MysqlStatement implements Closeable {
* @throws IllegalArgumentException if the parameter does not exist
* @see PreparedStatement#setBoolean(int, boolean)
*/
- public void setBoolean(String name, boolean value) throws SQLException {
- List<Integer> indexes = getIndexes(name);
- for (Integer index : indexes) {
- statement.setBoolean(index, value);
+ public void setBoolean( String name, boolean value ) throws SQLException
+ {
+ List<Integer> indexes = getIndexes( name );
+ for ( Integer index : indexes ) {
+ statement.setBoolean( index, value );
}
}
@@ -147,10 +159,11 @@ public class MysqlStatement implements Closeable {
* @throws IllegalArgumentException if the parameter does not exist
* @see PreparedStatement#setBoolean(int, boolean)
*/
- public void setBinary(String name, byte[] value) throws SQLException {
- List<Integer> indexes = getIndexes(name);
- for (Integer index : indexes) {
- statement.setBytes(index, value);
+ public void setBinary( String name, byte[] value ) throws SQLException
+ {
+ List<Integer> indexes = getIndexes( name );
+ for ( Integer index : indexes ) {
+ statement.setBytes( index, value );
}
}
@@ -161,7 +174,8 @@ public class MysqlStatement implements Closeable {
* @throws SQLException if an error occurred
* @see PreparedStatement#execute()
*/
- public boolean execute() throws SQLException {
+ public boolean execute() throws SQLException
+ {
return statement.execute();
}
@@ -172,9 +186,10 @@ public class MysqlStatement implements Closeable {
* @throws SQLException if an error occurred
* @see PreparedStatement#executeQuery()
*/
- public ResultSet executeQuery() throws SQLException {
+ public ResultSet executeQuery() throws SQLException
+ {
ResultSet rs = statement.executeQuery();
- openResultSets.add(rs);
+ openResultSets.add( rs );
return rs;
}
@@ -187,7 +202,8 @@ public class MysqlStatement implements Closeable {
* @throws SQLException if an error occurred
* @see PreparedStatement#executeUpdate()
*/
- public int executeUpdate() throws SQLException {
+ public int executeUpdate() throws SQLException
+ {
return statement.executeUpdate();
}
@@ -197,17 +213,18 @@ public class MysqlStatement implements Closeable {
* @see Statement#close()
*/
@Override
- public void close() {
- for (ResultSet rs : openResultSets) {
+ public void close()
+ {
+ for ( ResultSet rs : openResultSets ) {
try {
rs.close();
- } catch (SQLException e) {
+ } catch ( SQLException e ) {
//
}
}
try {
statement.close();
- } catch (SQLException e) {
+ } catch ( SQLException e ) {
// Nothing to do
}
}
@@ -217,7 +234,8 @@ public class MysqlStatement implements Closeable {
*
* @throws SQLException if something went wrong
*/
- public void addBatch() throws SQLException {
+ public void addBatch() throws SQLException
+ {
statement.addBatch();
}
@@ -229,91 +247,115 @@ public class MysqlStatement implements Closeable {
* @return update counts for each statement
* @throws SQLException if something went wrong
*/
- public int[] executeBatch() throws SQLException {
+ public int[] executeBatch() throws SQLException
+ {
return statement.executeBatch();
}
+ /**
+ * Get the generated key from the last insert. Assumes that one row was inserted, and the
+ * generated key is an int.
+ *
+ * @return the generated key
+ * @throws SQLException if no key was generated by this statement
+ */
+ public int getGeneratedKeys() throws SQLException
+ {
+ try ( ResultSet generatedKeys = statement.getGeneratedKeys() ) {
+ if ( generatedKeys.next() ) {
+ return generatedKeys.getInt( 1 );
+ }
+ throw new SQLException( "Could not obtain generated key" );
+ }
+ }
+
// static methods
- private static PreparsedQuery parse(String query) {
+ private static PreparsedQuery parse( String query )
+ {
int length = query.length();
- StringBuffer parsedQuery = new StringBuffer(length);
+ StringBuffer parsedQuery = new StringBuffer( length );
Map<String, List<Integer>> paramMap = new HashMap<>();
boolean inSingleQuote = false;
boolean inDoubleQuote = false;
boolean hasBackslash = false;
int index = 1;
- for (int i = 0; i < length; i++) {
- char c = query.charAt(i);
- if (hasBackslash) {
+ for ( int i = 0; i < length; i++ ) {
+ char c = query.charAt( i );
+ if ( hasBackslash ) {
// Last char was a backslash, so we ignore the current char
hasBackslash = false;
- } else if (c == '\\') {
+ } else if ( c == '\\' ) {
// This is a backslash, next char will be escaped
hasBackslash = true;
- } else if (inSingleQuote) {
+ } else if ( inSingleQuote ) {
// End of quoted string
- if (c == '\'') {
+ if ( c == '\'' ) {
inSingleQuote = false;
}
- } else if (inDoubleQuote) {
+ } else if ( inDoubleQuote ) {
// End of quoted string
- if (c == '"') {
+ if ( c == '"' ) {
inDoubleQuote = false;
}
} else {
// Not in string, look for named params
- if (c == '\'') {
+ if ( c == '\'' ) {
inSingleQuote = true;
- } else if (c == '"') {
+ } else if ( c == '"' ) {
inDoubleQuote = true;
- } else if (c == ':' && i + 1 < length && Character.isJavaIdentifierStart(query.charAt(i + 1))) {
+ } else if ( c == ':' && i + 1 < length && Character.isJavaIdentifierStart( query.charAt( i + 1 ) ) ) {
int j = i + 2;
- while (j < length && Character.isJavaIdentifierPart(query.charAt(j))) {
+ while ( j < length && Character.isJavaIdentifierPart( query.charAt( j ) ) ) {
j++;
}
- String name = query.substring(i + 1, j);
+ String name = query.substring( i + 1, j );
c = '?'; // replace the parameter with a question mark
i += name.length(); // skip past the end of the parameter
- List<Integer> indexList = paramMap.get(name);
- if (indexList == null) {
+ List<Integer> indexList = paramMap.get( name );
+ if ( indexList == null ) {
indexList = new ArrayList<>();
- paramMap.put(name, indexList);
+ paramMap.put( name, indexList );
}
- indexList.add(new Integer(index));
+ indexList.add( new Integer( index ) );
index++;
}
}
- parsedQuery.append(c);
+ parsedQuery.append( c );
}
- return new PreparsedQuery(parsedQuery.toString(), paramMap);
+ return new PreparsedQuery( parsedQuery.toString(), paramMap );
}
// private helper classes
- private static class PreparsedQuery {
+ private static class PreparsedQuery
+ {
private final Map<String, List<Integer>> indexMap;
private final String sql;
- public PreparsedQuery(String sql, Map<String, List<Integer>> indexMap) {
+ public PreparsedQuery( String sql, Map<String, List<Integer>> indexMap )
+ {
this.sql = sql;
this.indexMap = indexMap;
}
}
- private static class QueryCache extends LinkedHashMap<String, PreparsedQuery> {
+ private static class QueryCache extends LinkedHashMap<String, PreparsedQuery>
+ {
private static final long serialVersionUID = 1L;
- public QueryCache() {
- super(30, (float) 0.75, true);
+ public QueryCache()
+ {
+ super( 30, (float)0.75, true );
}
@Override
- protected boolean removeEldestEntry(Map.Entry<String, PreparsedQuery> eldest) {
+ protected boolean removeEldestEntry( Map.Entry<String, PreparsedQuery> eldest )
+ {
return size() > 40;
}
}
diff --git a/src/main/java/org/openslx/imagemaster/db/mappers/DbImage.java b/src/main/java/org/openslx/imagemaster/db/mappers/DbImage.java
index 027ddf6..2f5394c 100644
--- a/src/main/java/org/openslx/imagemaster/db/mappers/DbImage.java
+++ b/src/main/java/org/openslx/imagemaster/db/mappers/DbImage.java
@@ -1,11 +1,7 @@
package org.openslx.imagemaster.db.mappers;
-import java.util.List;
-
import org.openslx.bwlp.thrift.iface.ImagePublishData;
-import org.openslx.imagemaster.Globals;
-import org.openslx.imagemaster.serverconnection.UploadingImage;
-import org.openslx.imagemaster.util.Util;
+
/**
* Representing an image in the database.
@@ -14,221 +10,10 @@ import org.openslx.imagemaster.util.Util;
public class DbImage
{
- public final String uuid;
- public final int revision;
- public final String title;
- /**
- * Relative path of image file (relative to Globals.getImageDir())
- */
- public final String relativePath;
- public final long createTime;
- public final long updateTime;
- public final int ownerId;
- public final String ownerLogin;
- public final int operatingSystem;
- public final boolean isValid;
- public final boolean isDeleted;
- public final String longDescription;
- public final long fileSize;
- public final int[] blockStatus;
-
- public DbImage( String uuid )
- {
- this.uuid = uuid;
- this.revision = 0;
- this.title = null;
- this.relativePath = null;
- this.createTime = 0;
- this.updateTime = 0;
- this.ownerId = 0;
- this.ownerLogin = null;
- this.operatingSystem = 0;
- this.isValid = false;
- this.isDeleted = false;
- this.longDescription = null;
- this.fileSize = 0;
- this.blockStatus = null;
- }
-
- public DbImage( String uuid, int imageVersion, String imageName, String imagePath,
- long imageCreateTime, long imageUpdateTime, int imageOwnerId, String imageOwnerLogin, int contentOperatingSystem,
- boolean isValid, boolean isDeleted, String longDescription,
- long fileSize, String missingBlocksList )
- {
- this.uuid = uuid;
- this.revision = imageVersion;
- this.title = imageName;
- this.relativePath = imagePath;
- this.createTime = imageCreateTime;
- this.updateTime = imageUpdateTime;
- this.ownerId = imageOwnerId;
- this.ownerLogin = imageOwnerLogin;
- this.operatingSystem = contentOperatingSystem;
- this.isValid = isValid;
- this.isDeleted = isDeleted;
- this.longDescription = longDescription;
- this.fileSize = fileSize;
-
- String[] parts = missingBlocksList.split( ";" );
- blockStatus = new int[ Util.getNumberOfBlocks( fileSize, Globals.blockSize ) ]; // initialize array to ones
- for ( int i = 0; i < blockStatus.length; ++i ) {
- blockStatus[i] = UploadingImage.VALID;
- }
- for ( String block : parts ) { // Now mark missing blocks (if any)
- int i = Util.tryToParseInt( block, -1 );
- if ( i >= 0 && i < blockStatus.length )
- blockStatus[i] = UploadingImage.MISSING;
- }
- }
-
- /**
- * Check if image with imageData already exists. (Only checks the UUID.)
- *
- * @param imageData
- * @return
- */
- public static boolean exists( String uuid )
- {
- return getImageByUuid( uuid ) != null;
- }
-
- /**
- * Insert a new image into database
- *
- * @param imageData
- * The metadata of the image
- * @param filepath
- * Local storage path of image
- * @return Affected rows
- */
- public static int insert( ImagePublishData imageData, String filepath )
- {
- int numBlocks = Util.getNumberOfBlocks( imageData.fileSize, Globals.blockSize );
- String missingBlocksList = "";
- for ( int i = 0; i < numBlocks; i++ ) {
- missingBlocksList = missingBlocksList + String.valueOf( i ) + ";";
- }
- DbUser user = DbUser.forLogin( imageData.ownerLogin );
- int owner = 0;
- if ( user != null )
- owner = user.userId;
-
- return MySQL
- .update(
- "INSERT IGNORE INTO image (uuid, revision, title, path, createtime, updatetime, ownerid, operatingsystem, isvalid, isdeleted, description, filesize, missingblocks) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
- imageData.uuid, imageData.revision, imageData.title, filepath,
- imageData.createTime, imageData.updateTime, owner,
- imageData.operatingSystem, imageData.isValid,
- imageData.isDeleted, imageData.description, imageData.fileSize,
- missingBlocksList );
- }
-
- /**
- * Updates the missing blocks of an uploading image.
- *
- * @param missingBlocks
- * @return
- */
- public int updateMissingBlocks( List<Integer> missingBlocks )
- {
- String missingBlocksList = "";
- if ( missingBlocks != null ) {
- for ( Integer block : missingBlocks ) {
- missingBlocksList = missingBlocksList + String.valueOf( block ) + ";";
- }
- }
- return MySQL.update( "UPDATE image SET image.missingblocks = ? WHERE image.uuid = ?", missingBlocksList, uuid );
- }
-
- /**
- * Marks an image as _deleted_ in the database.
- *
- * @return
- */
- public int delete()
- {
- return MySQL.update( "UPDATE image SET image.isdeleted = 1 WHERE image.uuid = ?", this.uuid );
- }
-
- /**
- * Returns all images from database where blocks are still missing.
- *
- * @return
- */
- public static List<DbImage> getUploadingImages()
- {
- return MySQL
- .findAll(
- DbImage.class,
- "SELECT image.uuid, image.revision, image.title, image.path, image.createtime, image.updatetime, image.ownerid, user.login, image.operatingsystem, image.isvalid, image.isdeleted, image.description, image.filesize, image.missingblocks"
- + " FROM image"
- + " INNER JOIN user ON (image.ownerid = user.userid)"
- + " WHERE missingBlocks != ''" );
- }
-
- /**
- * Returns the image that is corrsponding to a specified uuid.
- *
- * @param uuid
- * @return
- */
- public static DbImage getImageByUuid( String uuid )
- {
- return MySQL
- .findUniqueOrNull(
- DbImage.class,
- "SELECT image.uuid, image.revision, image.title, image.path, image.createtime, image.updatetime, image.ownerid, user.login, image.operatingsystem, image.isvalid, image.isdeleted, image.description, image.filesize, image.missingblocks"
- + " FROM image"
- + " INNER JOIN user ON (image.ownerid = user.userid)"
- + " WHERE uuid = ?",
- uuid );
- }
-
- /**
- * Return all public images as ImageData list, which can be directly
- * used by the thrift API.
- *
- * @param start
- * @param limit
- * @return
- */
- public static List<ImageData> asImageDataList( int start, int limit )
- {
- return MySQL
- .findAll(
- ImageData.class,
- "SELECT image.uuid, image.revision, image.title, image.createtime, image.updatetime, user.login, image.operatingsystem, image.isvalid, image.isdeleted, image.description, image.filesize"
- + " FROM image"
- + " INNER JOIN user ON (image.ownerid = user.userid)"
- + " ORDER BY uuid, revision"
- + " LIMIT " + start + ", " + limit );
- }
-
- /**
- * Creates an instance of the thrift ImageData class of this DbImage object.
- *
- * @return The corresponding image data
- */
- public ImageData getImageData()
- {
- String owner = "unknown";
- DbUser user = DbUser.forLogin( this.ownerId );
- if ( user != null )
- owner = user.getLogin();
- return new ImageData(
- this.uuid, this.revision, this.title, this.createTime,
- this.updateTime, owner, this.operatingSystem, this.isValid,
- this.isDeleted, this.longDescription, this.fileSize );
- }
-
- /**
- * Get absolute path of this image
- *
- * @return absolute path
- */
- public String getAbsolutePath()
+ public static ImagePublishData getImageVersion( String imageVersionId )
{
- return Globals.getImageDir() + "/" + this.relativePath;
+ // TODO Auto-generated method stub
+ return null;
}
}
diff --git a/src/main/java/org/openslx/imagemaster/db/mappers/DbOperatingSystem.java b/src/main/java/org/openslx/imagemaster/db/mappers/DbOperatingSystem.java
deleted file mode 100644
index 1504e50..0000000
--- a/src/main/java/org/openslx/imagemaster/db/mappers/DbOperatingSystem.java
+++ /dev/null
@@ -1,36 +0,0 @@
-package org.openslx.imagemaster.db.mappers;
-
-import java.util.List;
-
-import org.openslx.bwlp.thrift.iface.OperatingSystem;
-import org.openslx.imagemaster.db.MySQL;
-import org.openslx.util.TimeoutReference;
-
-public class DbOperatingSystem
-{
-
- private static TimeoutReference<List<OperatingSystem>> cached = new TimeoutReference<List<OperatingSystem>>(
- 30000, null );
-
- private DbOperatingSystem()
- {
- }
-
- public static List<OperatingSystem> getAll()
- {
- List<OperatingSystem> list = cached.get();
- if ( list != null )
- return list;
- list = MySQL.findAll(
- OperatingSystem.class,
- "SELECT osid, displayname, NULL, architecture"
- + " FROM operatingsystem" );
- for ( OperatingSystem os : list ) {
- os.virtualizerOsId = MySQL.findMap( String.class, String.class,
- "SELECT virtid, virtoskeyword FROM os_x_virt WHERE osid = ?", os.osId );
- }
- cached.set( list );
- return list;
- }
-
-}
diff --git a/src/main/java/org/openslx/imagemaster/db/mappers/DbOsVirt.java b/src/main/java/org/openslx/imagemaster/db/mappers/DbOsVirt.java
new file mode 100644
index 0000000..ee8a39a
--- /dev/null
+++ b/src/main/java/org/openslx/imagemaster/db/mappers/DbOsVirt.java
@@ -0,0 +1,108 @@
+package org.openslx.imagemaster.db.mappers;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.log4j.Logger;
+import org.openslx.bwlp.thrift.iface.OperatingSystem;
+import org.openslx.bwlp.thrift.iface.Virtualizer;
+import org.openslx.imagemaster.db.Database;
+import org.openslx.imagemaster.db.MysqlConnection;
+import org.openslx.imagemaster.db.MysqlStatement;
+import org.openslx.imagemaster.util.Util;
+
+public class DbOsVirt
+{
+
+ private static final Logger LOGGER = Logger.getLogger( DbOsVirt.class );
+
+ public static List<OperatingSystem> getOsList() throws SQLException
+ {
+ try ( MysqlConnection connection = Database.getConnection() ) {
+ // Query OSs
+ MysqlStatement stmt = connection.prepareStatement( "SELECT"
+ + " osid, displayname, architecture, maxmem, maxcpu" + " FROM operatingsystem" );
+ ResultSet rs = stmt.executeQuery();
+ List<OperatingSystem> list = new ArrayList<>();
+ Map<Integer, Map<String, String>> osVirtMappings = getOsVirtMappings( connection );
+ while ( rs.next() ) {
+ int osId = rs.getInt( "osid" );
+ list.add( new OperatingSystem( osId, rs.getString( "displayname" ), osVirtMappings.get( osId ),
+ rs.getString( "architecture" ), rs.getInt( "maxmem" ), rs.getInt( "maxcpu" ) ) );
+ }
+ return list;
+ } catch ( SQLException e ) {
+ LOGGER.error( "Query failed in DbOsVirt.getOsList()", e );
+ throw e;
+ }
+ }
+
+ private static Map<Integer, Map<String, String>> getOsVirtMappings( MysqlConnection connection )
+ throws SQLException
+ {
+ MysqlStatement stmt = connection.prepareStatement( "SELECT osid, virtid, virtoskeyword FROM os_x_virt" );
+ ResultSet rs = stmt.executeQuery();
+ Map<Integer, Map<String, String>> map = new HashMap<>();
+ while ( rs.next() ) {
+ Integer osId = rs.getInt( "osid" );
+ Map<String, String> osMap = map.get( osId );
+ if ( osMap == null ) {
+ osMap = new HashMap<>();
+ map.put( osId, osMap );
+ }
+ osMap.put( rs.getString( "virtid" ), rs.getString( "virtoskeyword" ) );
+ }
+ return map;
+ }
+
+ public static List<Virtualizer> getVirtualizerList() throws SQLException
+ {
+ try ( MysqlConnection connection = Database.getConnection() ) {
+ MysqlStatement stmt = connection.prepareStatement( "SELECT virtid, virtname" + " FROM virtualizer" );
+ ResultSet rs = stmt.executeQuery();
+ List<Virtualizer> list = new ArrayList<>();
+ while ( rs.next() ) {
+ list.add( new Virtualizer( rs.getString( "virtid" ), rs.getString( "virtname" ) ) );
+ }
+ return list;
+ } catch ( SQLException e ) {
+ LOGGER.error( "Query failed in DbOsVirt.getVirtualizerList()", e );
+ throw e;
+ }
+ }
+
+ public static boolean osExists( int osId )
+ {
+ if ( osId <= 0 )
+ return false;
+ try ( MysqlConnection connection = Database.getConnection() ) {
+ MysqlStatement stmt = connection.prepareStatement( "SELECT osid FROM operatingsystem WHERE osid = :osid" );
+ stmt.setInt( "osid", osId );
+ ResultSet rs = stmt.executeQuery();
+ return rs.next();
+ } catch ( SQLException e ) {
+ LOGGER.error( "Query failed in DbOsVirt.exists()", e );
+ return false;
+ }
+ }
+
+ public static boolean virtExists( String virtId )
+ {
+ if ( Util.isEmpty( virtId ) )
+ return false;
+ try ( MysqlConnection connection = Database.getConnection() ) {
+ MysqlStatement stmt = connection.prepareStatement( "SELECT virtid FROM virtualizer WHERE virtid = :virtid" );
+ stmt.setString( "virtid", virtId );
+ ResultSet rs = stmt.executeQuery();
+ return rs.next();
+ } catch ( SQLException e ) {
+ LOGGER.error( "Query failed in DbOsVirt.virtExists()", e );
+ return false;
+ }
+ }
+
+}
diff --git a/src/main/java/org/openslx/imagemaster/db/mappers/DbPendingSatellite.java b/src/main/java/org/openslx/imagemaster/db/mappers/DbPendingSatellite.java
index 15cdbb9..906d49e 100644
--- a/src/main/java/org/openslx/imagemaster/db/mappers/DbPendingSatellite.java
+++ b/src/main/java/org/openslx/imagemaster/db/mappers/DbPendingSatellite.java
@@ -1,17 +1,54 @@
package org.openslx.imagemaster.db.mappers;
+import java.sql.SQLException;
+import java.util.List;
+
import org.apache.log4j.Logger;
-import org.openslx.imagemaster.db.MySQL;
+import org.openslx.bwlp.thrift.iface.UserInfo;
+import org.openslx.imagemaster.db.Database;
+import org.openslx.imagemaster.db.MysqlConnection;
+import org.openslx.imagemaster.db.MysqlStatement;
+import org.openslx.util.Json;
public class DbPendingSatellite
{
- private static final Logger LOG = Logger.getLogger( DbPendingSatellite.class );
+ private static final Logger LOGGER = Logger.getLogger( DbPendingSatellite.class );
+
+ public static int add( UserInfo user, String displayName, List<String> address, String modulus, String exponent )
+ throws SQLException
+ {
+ try ( MysqlConnection connection = Database.getConnection() ) {
+ MysqlStatement stmt = connection.prepareStatement( "INSERT INTO pending_satellite"
+ + " (dateline, userid, organizationid, satellitename, addresses, publickey)"
+ + " VALUES (UNIX_TIMESTAMP(), :userid, :organizationid, :satellitename, :addresses, :pubkey)", true );
+ stmt.setString( "userid", user.userId );
+ stmt.setString( "organizationid", user.organizationId );
+ stmt.setString( "satellitename", displayName );
+ stmt.setString( "addresses", Json.serialize( address ) );
+ stmt.setString( "pubkey", Json.serialize( new KeyWrapper( modulus, exponent ) ) );
+ stmt.executeUpdate();
+ int key = stmt.getGeneratedKeys();
+ connection.commit();
+ return key;
+ } catch ( SQLException e ) {
+ LOGGER.error( "Query failed in DbPendingSatellite.add()", e );
+ throw e;
+ }
+ }
- public static boolean add( String organizationId, String address, String modulus, String exponent )
+ private static class KeyWrapper
{
- String publickey = "mod:" + modulus + " exp:" + exponent;
- return MySQL.update( "INSERT INTO pending_satellite (dateline, organizationid, address, publickey)"
- + " VALUES (UNIX_TIMESTAMP(), ?, ?, ?)", organizationId, address, publickey ) != 0;
+ public String type;
+ public String modulus;
+ public String exponent;
+
+ public KeyWrapper( String modulus, String exponent )
+ {
+ this.type = "RSA";
+ this.modulus = modulus;
+ this.exponent = exponent;
+ }
}
+
}
diff --git a/src/main/java/org/openslx/imagemaster/db/mappers/DbSatellite.java b/src/main/java/org/openslx/imagemaster/db/mappers/DbSatellite.java
new file mode 100644
index 0000000..811ac67
--- /dev/null
+++ b/src/main/java/org/openslx/imagemaster/db/mappers/DbSatellite.java
@@ -0,0 +1,62 @@
+package org.openslx.imagemaster.db.mappers;
+
+import java.nio.ByteBuffer;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.openslx.bwlp.thrift.iface.Satellite;
+import org.openslx.bwlp.thrift.iface.UserInfo;
+import org.openslx.imagemaster.db.Database;
+import org.openslx.imagemaster.db.MysqlConnection;
+import org.openslx.imagemaster.db.MysqlStatement;
+import org.openslx.imagemaster.db.models.LocalSatellite;
+import org.openslx.util.Json;
+
+public class DbSatellite
+{
+
+ private static final Logger LOGGER = Logger.getLogger( DbSatellite.class );
+
+ public static LocalSatellite get( int satelliteId )
+ {
+ return null;
+ }
+
+ public static List<Satellite> getSatellites( UserInfo ui ) throws SQLException
+ {
+ if ( ui == null )
+ return null;
+ return getSatellites( ui.organizationId );
+ }
+
+ public static List<Satellite> getSatellites( String organizationId ) throws SQLException
+ {
+ if ( organizationId == null )
+ return null;
+ try ( MysqlConnection connection = Database.getConnection() ) {
+ MysqlStatement stmt = connection.prepareStatement( "SELECT satellitename, addresses, certsha256"
+ + " FROM satellite WHERE organizationid = :organizationid" );
+ stmt.setString( "organizationid", organizationId );
+ ResultSet rs = stmt.executeQuery();
+ List<Satellite> list = new ArrayList<>();
+ while ( rs.next() ) {
+ List<String> al = Arrays.asList( Json.deserialize( rs.getString( "addresses" ), String[].class ) );
+ list.add( new Satellite( al, rs.getString( "satellitename" ), ByteBuffer.wrap( rs.getBytes( "certsha256" ) ) ) );
+ }
+ return list;
+ } catch ( SQLException e ) {
+ LOGGER.error( "Query failed in DbSatellite.getSatellites()", e );
+ throw e;
+ }
+ }
+
+ public static LocalSatellite get( String organizationId, String displayName )
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+}
diff --git a/src/main/java/org/openslx/imagemaster/db/mappers/DbUser.java b/src/main/java/org/openslx/imagemaster/db/mappers/DbUser.java
index ed55d8a..349b023 100644
--- a/src/main/java/org/openslx/imagemaster/db/mappers/DbUser.java
+++ b/src/main/java/org/openslx/imagemaster/db/mappers/DbUser.java
@@ -105,4 +105,22 @@ public class DbUser
return new ArrayList<>( 0 );
}
+ public static boolean exists( UserInfo user )
+ {
+ if ( user == null )
+ return false;
+ return exists( user.userId );
+ }
+
+ private static boolean exists( String userId )
+ {
+ if ( userId == null )
+ return false;
+ try {
+ return forLogin( userId ) != null;
+ } catch ( SQLException e ) {
+ return false;
+ }
+ }
+
}
diff --git a/src/main/java/org/openslx/imagemaster/db/models/LocalSatellite.java b/src/main/java/org/openslx/imagemaster/db/models/LocalSatellite.java
new file mode 100644
index 0000000..75f0f94
--- /dev/null
+++ b/src/main/java/org/openslx/imagemaster/db/models/LocalSatellite.java
@@ -0,0 +1,22 @@
+package org.openslx.imagemaster.db.models;
+
+import java.security.Key;
+import java.util.List;
+
+public class LocalSatellite
+{
+ // TODO
+ public final int satelliteId = 0;
+ public final String satelliteName = "";
+ public final String organizationId = "";
+ public final List<String> addresses = null;
+
+ // TODO
+
+ public Key getPubkey()
+ {
+ // TODO
+ return null;
+ }
+
+}