bwLehrpool Masterserver
Manages authentication and sharing of virtual machines between participating institutions
DbOsVirt.java
Go to the documentation of this file.
1 package org.openslx.imagemaster.db.mappers;
2 
3 import java.sql.ResultSet;
4 import java.sql.SQLException;
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9 
10 import org.apache.logging.log4j.LogManager;
11 import org.apache.logging.log4j.Logger;
12 import org.openslx.bwlp.thrift.iface.OperatingSystem;
13 import org.openslx.bwlp.thrift.iface.Virtualizer;
18 
19 public class DbOsVirt
20 {
21 
22  private static final Logger LOGGER = LogManager.getLogger( DbOsVirt.class );
23 
24  public static List<OperatingSystem> getOsList() throws SQLException
25  {
26  try ( MysqlConnection connection = Database.getConnection() ) {
27  // Query OSs
28  MysqlStatement stmt = connection.prepareStatement( "SELECT"
29  + " osid, displayname, architecture, maxmem, maxcpu" + " FROM operatingsystem" );
30  ResultSet rs = stmt.executeQuery();
31  List<OperatingSystem> list = new ArrayList<>();
32  Map<Integer, Map<String, String>> osVirtMappings = getOsVirtMappings( connection );
33  while ( rs.next() ) {
34  int osId = rs.getInt( "osid" );
35  list.add( new OperatingSystem( osId, rs.getString( "displayname" ), osVirtMappings.get( osId ),
36  rs.getString( "architecture" ), rs.getInt( "maxmem" ), rs.getInt( "maxcpu" ) ) );
37  }
38  return list;
39  } catch ( SQLException e ) {
40  LOGGER.error( "Query failed in DbOsVirt.getOsList()", e );
41  throw e;
42  }
43  }
44 
45  private static Map<Integer, Map<String, String>> getOsVirtMappings( MysqlConnection connection )
46  throws SQLException
47  {
48  MysqlStatement stmt = connection.prepareStatement( "SELECT osid, virtid, virtoskeyword FROM os_x_virt" );
49  ResultSet rs = stmt.executeQuery();
50  Map<Integer, Map<String, String>> map = new HashMap<>();
51  while ( rs.next() ) {
52  Integer osId = rs.getInt( "osid" );
53  Map<String, String> osMap = map.get( osId );
54  if ( osMap == null ) {
55  osMap = new HashMap<>();
56  map.put( osId, osMap );
57  }
58  osMap.put( rs.getString( "virtid" ), rs.getString( "virtoskeyword" ) );
59  }
60  return map;
61  }
62 
63  public static List<Virtualizer> getVirtualizerList() throws SQLException
64  {
65  try ( MysqlConnection connection = Database.getConnection() ) {
66  MysqlStatement stmt = connection.prepareStatement( "SELECT virtid, virtname" + " FROM virtualizer" );
67  ResultSet rs = stmt.executeQuery();
68  List<Virtualizer> list = new ArrayList<>();
69  while ( rs.next() ) {
70  list.add( new Virtualizer( rs.getString( "virtid" ), rs.getString( "virtname" ) ) );
71  }
72  return list;
73  } catch ( SQLException e ) {
74  LOGGER.error( "Query failed in DbOsVirt.getVirtualizerList()", e );
75  throw e;
76  }
77  }
78 
79  public static boolean osExists( int osId )
80  {
81  if ( osId <= 0 )
82  return false;
83  try ( MysqlConnection connection = Database.getConnection() ) {
84  MysqlStatement stmt = connection.prepareStatement( "SELECT osid FROM operatingsystem WHERE osid = :osid" );
85  stmt.setInt( "osid", osId );
86  ResultSet rs = stmt.executeQuery();
87  return rs.next();
88  } catch ( SQLException e ) {
89  LOGGER.error( "Query failed in DbOsVirt.exists()", e );
90  return false;
91  }
92  }
93 
94  public static boolean virtExists( String virtId )
95  {
96  if ( Util.isEmpty( virtId ) )
97  return false;
98  try ( MysqlConnection connection = Database.getConnection() ) {
99  MysqlStatement stmt = connection.prepareStatement( "SELECT virtid FROM virtualizer WHERE virtid = :virtid" );
100  stmt.setString( "virtid", virtId );
101  ResultSet rs = stmt.executeQuery();
102  return rs.next();
103  } catch ( SQLException e ) {
104  LOGGER.error( "Query failed in DbOsVirt.virtExists()", e );
105  return false;
106  }
107  }
108 
109 }
static List< Virtualizer > getVirtualizerList()
Definition: DbOsVirt.java:63
static MysqlConnection getConnection()
Get a connection to the database.
Definition: Database.java:92
ResultSet executeQuery()
Executes the statement, which must be a query.
static List< OperatingSystem > getOsList()
Definition: DbOsVirt.java:24
void setInt(String name, int value)
Sets a parameter.
void setString(String name, String value)
Sets a parameter.
static boolean virtExists(String virtId)
Definition: DbOsVirt.java:94
Class for creating PreparedStatements with named parameters.
Some utilities to make our lives easier.
Definition: Util.java:18
static Map< Integer, Map< String, String > > getOsVirtMappings(MysqlConnection connection)
Definition: DbOsVirt.java:45
static boolean isEmpty(String str)
Definition: Util.java:123