Skip to content

Commit

Permalink
ARTEMIS-4181 make try-with-resources style consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
jbertram committed Mar 3, 2023
1 parent 5d024b7 commit 97c7852
Show file tree
Hide file tree
Showing 23 changed files with 151 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ private void upgradeLogging(ActionContext context, File etcFolder, File bkpFolde
if (!newLogging.exists()) {
context.out.println("Creating " + newLogging);
try (InputStream inputStream = openStream("etc/" + Create.ETC_LOG4J2_PROPERTIES);
OutputStream outputStream = new FileOutputStream(newLogging);) {
OutputStream outputStream = new FileOutputStream(newLogging)) {
copy(inputStream, outputStream);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ public static boolean waitForServerToStart(String uri, long timeout) throws Inte
public static boolean waitForServerToStart(String uri, String username, String password, long timeout) throws InterruptedException {
long realTimeout = System.currentTimeMillis() + timeout;
while (System.currentTimeMillis() < realTimeout) {
try (ActiveMQConnectionFactory cf = ActiveMQJMSClient.createConnectionFactory(uri, null); Connection c = cf.createConnection(username, password)) {
try (ActiveMQConnectionFactory cf = ActiveMQJMSClient.createConnectionFactory(uri, null);
Connection c = cf.createConnection(username, password)) {
System.out.println("server " + uri + " started");
} catch (Exception e) {
System.out.println("awaiting server " + uri + " start at ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1582,7 +1582,8 @@ public void testQstat() throws Exception {
System.setProperty("artemis.instance", instanceQstat.getAbsolutePath());
Artemis.internalExecute("run");

try (ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616"); Connection connection = cf.createConnection("admin", "admin");) {
try (ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = cf.createConnection("admin", "admin")) {

//set up some queues with messages and consumers
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Expand Down Expand Up @@ -1858,7 +1859,8 @@ public void testQstatColumnWidth() throws Exception {
System.setProperty("artemis.instance", instanceQstat.getAbsolutePath());
Artemis.internalExecute("run");

try (ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616"); Connection connection = cf.createConnection("admin", "admin");) {
try (ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = cf.createConnection("admin", "admin")) {

//set up some queues with messages and consumers
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Expand Down Expand Up @@ -2035,7 +2037,8 @@ public void testQstatWarnings() throws Exception {
System.setProperty("artemis.instance", instanceQstat.getAbsolutePath());
Artemis.internalExecute("run");

try (ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616"); Connection connection = cf.createConnection("admin", "admin");) {
try (ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = cf.createConnection("admin", "admin")) {

TestActionContext context;
StatQueue statQueue;
Expand Down Expand Up @@ -2119,7 +2122,8 @@ public void testRunPropertiesArgumentSetsAcceptorPort() throws Exception {
Artemis.internalExecute("run", "--properties", new File(brokerPropertiesFromClasspath.toURI()).getAbsolutePath());

// verify
try (ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61618"); Connection connection = cf.createConnection("admin", "admin");) {
try (ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61618");
Connection connection = cf.createConnection("admin", "admin")) {
connection.start();
} finally {
stopServer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ private void internalTestSendDirectToQueue(RoutingType routingType) throws Excep
createQueue(routingType, address, queue1Name);
createQueue(routingType, address, queue2Name);

try (ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616"); Connection connection = cf.createConnection("admin", "admin");) {
try (ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = cf.createConnection("admin", "admin")) {

// send messages to queue
Session session = createSession(connection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,8 @@ private CoreObjectMessageWrapper createObjectMessage() {
private CoreObjectMessageWrapper createObjectMessage(Serializable payload) {
CoreObjectMessageWrapper result = AMQPMessageSupport.createObjectMessage(0, null);

try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos);) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos)) {

oos.writeObject(payload);
byte[] data = baos.toByteArray();
Expand Down Expand Up @@ -589,7 +590,8 @@ private CoreTextMessageWrapper createTextMessage(String text) {
}

private Object deserialize(byte[] payload) throws Exception {
try (ByteArrayInputStream bis = new ByteArrayInputStream(payload); ObjectInputStream ois = new ObjectInputStream(bis);) {
try (ByteArrayInputStream bis = new ByteArrayInputStream(payload);
ObjectInputStream ois = new ObjectInputStream(bis)) {

return ois.readObject();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,8 @@ private static byte[] toAMQMessageDefaultType(final ActiveMQBuffer buffer,
byte[] bytes = new byte[n];
buffer.readBytes(bytes);
if (isCompressed) {
try (ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); DeflaterOutputStream out = new DeflaterOutputStream(bytesOut, true)) {
try (ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
DeflaterOutputStream out = new DeflaterOutputStream(bytesOut, true)) {
out.write(bytes);
out.flush();
bytes = bytesOut.toByteArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,8 @@ public boolean accept(File file, String s) {
if (files != null && files.length > 0) {
Arrays.sort(files);
for (String fileName : files) {
try (FileInputStream fileInputStream = new FileInputStream(new File(dir, fileName)); BufferedInputStream reader = new BufferedInputStream(fileInputStream)) {
try (FileInputStream fileInputStream = new FileInputStream(new File(dir, fileName));
BufferedInputStream reader = new BufferedInputStream(fileInputStream)) {
brokerProperties.clear();
brokerProperties.load(reader);
parsePrefixedProperties(fileName, brokerProperties, null);
Expand All @@ -532,7 +533,8 @@ public boolean accept(File file, String s) {
}
} else {
File file = new File(fileUrl);
try (FileInputStream fileInputStream = new FileInputStream(file); BufferedInputStream reader = new BufferedInputStream(fileInputStream)) {
try (FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream reader = new BufferedInputStream(fileInputStream)) {
brokerProperties.load(reader);
parsePrefixedProperties(file.getName(), brokerProperties, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,8 @@ private void sendLargeFile(AbstractJournalStorageManager.JournalContent content,
final ReusableLatch flushed = new ReusableLatch(1);

try {
try (FileInputStream fis = new FileInputStream(file.getJavaFile()); FileChannel channel = fis.getChannel()) {
try (FileInputStream fis = new FileInputStream(file.getJavaFile());
FileChannel channel = fis.getChannel()) {

// We cannot afford having a single buffer here for this entire loop
// because sendReplicatePacket will encode the packet as a NettyBuffer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ public void testLoginReload() throws Exception {
private String genHash(File usersFile) {
Checksum hash = new Adler32();

try (FileReader fileReader = new FileReader(usersFile); BufferedReader bufferedReader = new BufferedReader(fileReader)) {
try (FileReader fileReader = new FileReader(usersFile);
BufferedReader bufferedReader = new BufferedReader(fileReader)) {
String line = null;
while ((line = bufferedReader.readLine()) != null) {
if (!line.startsWith("#") && !line.isBlank()) {
Expand Down
2 changes: 2 additions & 0 deletions etc/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,7 @@ under the License.
<property name="requiredParameters" value="name"/>
</module>

<module name="UnnecessarySemicolonInTryWithResources"/>

</module>
</module>
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ private static void validateAlias(KeyStore store, String alias) throws IllegalAr

private static KeyStore loadStore(String storePath, final String password, String storeType) throws Exception {
KeyStore store = KeyStore.getInstance(storeType);
try (InputStream in = new FileInputStream(new File(storePath));) {
try (InputStream in = new FileInputStream(new File(storePath))) {
store.load(in, password != null ? password.toCharArray() : null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ public void testApplicationPropertiesFromTransformerForwardBridge() throws Excep

sendMessages("uswest.Provider.AMC.Agent.f261d0fa-51bd-44bd-abe0-ce22d2a387cd.CustomNotification", 1, RoutingType.ANYCAST, true);

try (ServerLocator locator = ActiveMQClient.createServerLocator(getServer1URL()); ClientSessionFactory sessionFactory = locator.createSessionFactory(); ClientSession session = sessionFactory.createSession(); ClientConsumer consumer = session.createConsumer(notificationsQueue)) {
try (ServerLocator locator = ActiveMQClient.createServerLocator(getServer1URL());
ClientSessionFactory sessionFactory = locator.createSessionFactory();
ClientSession session = sessionFactory.createSession();
ClientConsumer consumer = session.createConsumer(notificationsQueue)) {

session.start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ public void tearDown() throws Exception {

@Test
public void testSendMessageToBroker0GetFromBroker1() throws Exception {
try (ServerLocator locator = ActiveMQClient.createServerLocator(getServer1URL()); ClientSessionFactory sessionFactory = locator.createSessionFactory(); ClientSession session = sessionFactory.createSession(); ClientConsumer consumer = session.createConsumer(notificationsQueue)) {
try (ServerLocator locator = ActiveMQClient.createServerLocator(getServer1URL());
ClientSessionFactory sessionFactory = locator.createSessionFactory();
ClientSession session = sessionFactory.createSession();
ClientConsumer consumer = session.createConsumer(notificationsQueue)) {

session.start();

Expand All @@ -187,7 +190,10 @@ public void testSendMessageToBroker0GetFromBroker1() throws Exception {

@Test
public void testSendMessageToBroker0GetFromBroker2() throws Exception {
try (ServerLocator locator = ActiveMQClient.createServerLocator(getServer2URL()); ClientSessionFactory sessionFactory = locator.createSessionFactory(); ClientSession session = sessionFactory.createSession(); ClientConsumer consumer = session.createConsumer(notificationsQueue)) {
try (ServerLocator locator = ActiveMQClient.createServerLocator(getServer2URL());
ClientSessionFactory sessionFactory = locator.createSessionFactory();
ClientSession session = sessionFactory.createSession();
ClientConsumer consumer = session.createConsumer(notificationsQueue)) {

session.start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,12 +439,16 @@ private void jmsTest(boolean persistent, boolean tx) throws JMSException {
int MESSAGES = 10;
String producerUri = "amqp://localhost:5672";
final JmsConnectionFactory producerFactory = new JmsConnectionFactory(producerUri);
try (Connection producerConnection = producerFactory.createConnection(); Session producerSession = producerConnection.createSession(tx, tx ? Session.SESSION_TRANSACTED : Session.AUTO_ACKNOWLEDGE)) {
try (Connection producerConnection = producerFactory.createConnection();
Session producerSession = producerConnection.createSession(tx, tx ? Session.SESSION_TRANSACTED : Session.AUTO_ACKNOWLEDGE)) {
producerConnection.start();
final Destination queue = producerSession.createQueue(getQueueName());
String consumerUri = "amqp://localhost:5672";
final JmsConnectionFactory consumerConnectionFactory = new JmsConnectionFactory(consumerUri);
try (Connection consumerConnection = consumerConnectionFactory.createConnection(); Session consumerSession = consumerConnection.createSession(tx, tx ? Session.SESSION_TRANSACTED : Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = consumerSession.createConsumer(queue); MessageProducer producer = producerSession.createProducer(queue)) {
try (Connection consumerConnection = consumerConnectionFactory.createConnection();
Session consumerSession = consumerConnection.createSession(tx, tx ? Session.SESSION_TRANSACTED : Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = consumerSession.createConsumer(queue);
MessageProducer producer = producerSession.createProducer(queue)) {
if (persistent) {
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ public void testDownstreamFederatedAddressReplicationRefOneWayTransformer() thro
private void verifyTransformer(String address) throws Exception {
ConnectionFactory cf1 = getCF(1);
ConnectionFactory cf0 = getCF(0);
try (Connection connection1 = cf1.createConnection(); Connection connection0 = cf0.createConnection()) {
try (Connection connection1 = cf1.createConnection();
Connection connection0 = cf0.createConnection()) {
connection1.start();
connection0.start();

Expand Down Expand Up @@ -343,7 +344,8 @@ protected void testFederatedAddressDivert(boolean downstream, boolean addressFir

ConnectionFactory cf1 = getCF(1);
ConnectionFactory cf0 = getCF(0);
try (Connection connection1 = cf1.createConnection(); Connection connection0 = cf0.createConnection()) {
try (Connection connection1 = cf1.createConnection();
Connection connection0 = cf0.createConnection()) {
connection1.start();
connection0.start();

Expand Down Expand Up @@ -395,7 +397,8 @@ private void testFederatedAddressReplication(String address) throws Exception {

ConnectionFactory cf1 = getCF(1);
ConnectionFactory cf0 = getCF(0);
try (Connection connection1 = cf1.createConnection(); Connection connection0 = cf0.createConnection()) {
try (Connection connection1 = cf1.createConnection();
Connection connection0 = cf0.createConnection()) {
connection1.start();
connection0.start();

Expand Down Expand Up @@ -452,7 +455,8 @@ public void testFederatedAddressDeployAfterQueuesExist() throws Exception {

ConnectionFactory cf1 = getCF(1);
ConnectionFactory cf0 = getCF(0);
try (Connection connection1 = cf1.createConnection(); Connection connection0 = cf0.createConnection()) {
try (Connection connection1 = cf1.createConnection();
Connection connection0 = cf0.createConnection()) {
connection1.start();
connection0.start();

Expand Down Expand Up @@ -492,7 +496,8 @@ public void testFederatedAddressRemoteBrokerRestart() throws Exception {

ConnectionFactory cf1 = getCF(1);
ConnectionFactory cf0 = getCF(0);
try (Connection connection1 = cf1.createConnection(); Connection connection0 = cf0.createConnection()) {
try (Connection connection1 = cf1.createConnection();
Connection connection0 = cf0.createConnection()) {
connection1.start();
connection0.start();

Expand Down Expand Up @@ -550,7 +555,8 @@ public void testFederatedAddressLocalBrokerRestart() throws Exception {

ConnectionFactory cf1 = getCF(1);
ConnectionFactory cf0 = getCF(0);
try (Connection connection1 = cf1.createConnection(); Connection connection0 = cf0.createConnection()) {
try (Connection connection1 = cf1.createConnection();
Connection connection0 = cf0.createConnection()) {
connection1.start();
connection0.start();

Expand Down Expand Up @@ -624,7 +630,8 @@ public void testFederatedAddressChainOfBrokers() throws Exception {

ConnectionFactory cf2 = getCF(2);
ConnectionFactory cf0 = getCF(0);
try (Connection connection2 = cf2.createConnection(); Connection connection0 = cf0.createConnection()) {
try (Connection connection2 = cf2.createConnection();
Connection connection0 = cf0.createConnection()) {
connection0.start();
Session session0 = connection0.createSession();
Topic topic0 = session0.createTopic(address);
Expand Down

0 comments on commit 97c7852

Please sign in to comment.